如何在地图上显示路线

时间:2014-10-10 09:41:22

标签: android google-maps map

我想创建一个显示地图的Activity。当我点击按钮时,我将LatLng点保存到列表中。所以我走在街上,当我走路的时候,我点击按钮,我想在地图上显示我的路线。所以我尝试编写这段代码,但没找到:

public void settaMappa(View view){
        LocationResult locationResult = new LocationResult(){
            @Override
            public void gotLocation(Location location){
                //Got the location!
                System.out.println("ho avuto un segnale gps valido ");
                mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
                if(listaPuntiTerreno == null)
                    listaPuntiTerreno = new ArrayList<LatLng>();
                if (location != null)
                 {

                    LatLng latLong = new LatLng(location.getLatitude(), location.getLongitude());
                     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 13));

                     CameraPosition cameraPosition = new CameraPosition.Builder()
                     .target(new LatLng(location.getLatitude(), location.getLongitude()))      // Sets the center of the map to location user
                     .zoom(17)                   // Sets the zoom
                     .bearing(90)                // Sets the orientation of the camera to east
                     .tilt(40)                   // Sets the tilt of the camera to 30 degrees
                     .build();                   // Creates a CameraPosition from the builder
                     mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
                     listaPuntiTerreno.add(latLong);
                     disegnaTracciato();
                 }
            }
        };
        MyLocation myLocation = new MyLocation();
        myLocation.getLocation(this, locationResult);
    }


   public void disegnaTracciato(){
        if(listaPuntiTerreno !=null &&
                listaPuntiTerreno.size()>1){

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();

            PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
            for (int z = 0; z < listaPuntiTerreno.size(); z++) {
                LatLng point = listaPuntiTerreno.get(z);
                options.add(point);
            }
            Polyline line = mMap.addPolyline(options);
        }
    }

我们可以帮助我吗?

最佳制度

1 个答案:

答案 0 :(得分:1)

我已解决了我的问题,代码是这样的:

public MyLocation myLocation;
    /**
     * il metodo consente di registrare il punto
     */
    LocationResult locationResult = new LocationResult(){
        @Override
        public void gotLocation(Location location){
            //Got the location!
            System.out.println("ho avuto un segnale gps valido ");

            if (location != null)
            {
                LatLng latLong = new LatLng(location.getMyLocation().getLatitude(),
                        location.getMyLocation().getLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong, 25));
                listaPuntiTerreno.add(latLong);
                Toast toast = Toast.makeText(getApplicationContext(), "Punto registrato correttamente", Toast.LENGTH_SHORT);
                toast.show();
                disegnaTracciato();
            }
        }
    };

当我点击按钮时,我会调用此方法:

public void registraPunto(View view){
   myLocation.getLocation(this, locationResult);
}

这是一个MyLocation类(我从另一个帖子复制了这个类)

public class MyLocation {
    Timer timer1;
    LocationManager lm;
    LocationResult locationResult;
    boolean gps_enabled=false;
    boolean network_enabled=false;
    private ProgressDialog pDialog;
    private final static int LOCALIZATION_DELAY = 10000;

    public boolean getLocation(Context context, LocationResult result)
    {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        locationResult=result;
        if(lm==null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){}
        try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){}

        //don't start listeners if no provider is enabled
        if(!gps_enabled && !network_enabled)
            return false;

        if(gps_enabled)
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        if(network_enabled)
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);

        pDialog = ProgressDialog.show(context, "Attendere ...", "Localizzazione in corso ...", true);
        pDialog.setCancelable(false);

        timer1=new Timer();
        timer1.schedule(new GetLastLocation(), LOCALIZATION_DELAY);
        return true;
    }

    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            if(pDialog.isShowing())
                pDialog.dismiss();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            if(pDialog.isShowing())
                pDialog.dismiss();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             if(pDialog.isShowing())
                pDialog.dismiss();

             lm.removeUpdates(locationListenerGps);
             lm.removeUpdates(locationListenerNetwork);

             Location net_loc=null, gps_loc=null;
             if(gps_enabled)
                 gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
             if(network_enabled)
                 net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

             //if there are both values use the latest one
             if(gps_loc!=null && net_loc!=null){
                 if(gps_loc.getTime()>net_loc.getTime())
                     locationResult.gotLocation(gps_loc);
                 else
                     locationResult.gotLocation(net_loc);
                 return;
             }

             if(gps_loc!=null){
                 locationResult.gotLocation(gps_loc);
                 return;
             }

             if(net_loc!=null){
                 locationResult.gotLocation(net_loc);
                 return;
             }

             locationResult.gotLocation(null);
        }
    }

    public static abstract class LocationResult{
        public abstract void gotLocation(Location location);
    }
}

现在,当我尝试单击按钮时,我从MyLocation类获得了一个位置,但经度和纬度是错误的。如果我在maps.google.it上插入此号码,我会看到我的真实位置是错误的,如果我从mMap对象获得经度和纬度,这些都是正确的。

我的错误在哪里? 我们可以帮助我吗?