Android 如何在谷歌地图上绘制两点之间的路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21154758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:02:50  来源:igfitidea点击:

how to draw path between 2 points on google map

androidgoogle-maps

提问by Breaking code

I have been working on an android application which uses Google map.Now I want to generate the path (driving direction) between 2 points on the map,how can this be done?

我一直在开发一个使用谷歌地图的安卓应用程序。现在我想生成地图上两点之间的路径(行车方向),怎么做?

回答by vishal yadav

public  void DrawLine(LatLng location){
    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.add(location)
    .add(new LatLng(mlatitude, mlongitude))
            .add(new LatLng(mlatitudeEnd,mlongitudeEND));
         mMap.addPolyline(polylineOptions);


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

}

@Override
protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;     
}


protected void placeMarkerOnMap(LatLng location){
    MarkerOptions markerOptions=new MarkerOptions().position(location);
    String str_getloc = getAddress(location);
    markerOptions.title(str_getloc);
    mMap.addMarker(markerOptions);
}
private String getAddress(LatLng location){
    Geocoder geocoder=new Geocoder(this);
    String addresstxt="";
    List<Address> addresses=null;
    Address address=null;

    try {
        addresses=geocoder.getFromLocation(location.latitude,location.longitude,1);
        //addresstxt= String.valueOf((new LatLng(mlatitude,mlongitude)));
        //addresses.add(addresstxt)
        if (null != addresses && !addresses.isEmpty() ){
            address=addresses.get(0);
            for (int i=0; i<address.getMaxAddressLineIndex();i++){
                addresstxt += (i==0) ?address.getAddressLine(i): ("\n"+address.getAddressLine(i));

            }
            if (mlatitudeEnd!=0.0&&mlongitudeEND!=0.0){
                Toast.makeText(this, "if", Toast.LENGTH_SHORT).show();
                // DrawLine(new LatLng(mlatitude,mlongitude));
                DrawLine(location);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return addresstxt;
}