Android 如何在地图上显示路线

Android 如何在地图上显示路线,android,google-maps,map,Android,Google Maps,Map,我想创建一个显示地图的活动。当我点击一个按钮时,我会将一个锁定点保存到列表中。所以我走在街上,当我走的时候,我点击按钮,我想在地图上显示我的路线。因此,我尝试编写此代码,但未找到: public void settaMappa(View view){ LocationResult locationResult = new LocationResult(){ @Override public void gotLocation(Loca

我想创建一个显示地图的活动。当我点击一个按钮时,我会将一个锁定点保存到列表中。所以我走在街上,当我走的时候,我点击按钮,我想在地图上显示我的路线。因此,我尝试编写此代码,但未找到:

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);
        }
    }
public void settaMappa(视图){
LocationResult LocationResult=新的LocationResult(){
@凌驾
公共位置(位置){
//找到位置了!
系统输出打印(“ho avuto un segnale gps valido”);
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
if(listaPuntiTerreno==null)
listaPuntiTerreno=newarraylist();
如果(位置!=null)
{
LatLng latLong=新LatLng(location.getLatitude(),location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLong,13));
CameraPosition CameraPosition=新建CameraPosition.Builder()
.target(新LatLng(location.getLatitude(),location.getLongitude())//将地图的中心设置为用户位置
.zoom(17)//设置缩放
.bearing(90)//将相机的方向设置为东
.tilt(40)//将相机的倾斜设置为30度
.build();//从生成器创建CameraPosition
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
listaPuntiTerreno.add(latLong);
脱钩();
}
}
};
MyLocation MyLocation=新的MyLocation();
myLocation.getLocation(这是locationResult);
}
公共无效解除签名(){
if(listaPuntiTerreno!=null&&
listaPuntiTerreno.size()>1){
mMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map2)).getMap();
PolylineOptions=new PolylineOptions().width(5).color(color.BLUE).测地线(true);
对于(int z=0;z
我们能帮我吗


最佳规范

我已经解决了我的问题,代码如下:

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(我从另一篇文章中复制了这个类)

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

我的错误在哪里?
我们能帮我吗?

你到底有什么问题?想用你的特定路线设置你当前位置的动画吗?问题是:当我输入地图多段线选项,但在地图上看不到这些选项时。
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);
    }
}