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

时间:2014-01-16 06:27:58

标签: android google-maps

我一直在使用谷歌地图的Android应用程序。现在我想生成地图上2点之间的路径(行车方向),怎么办呢?

2 个答案:

答案 0 :(得分:4)

答案 1 :(得分:0)

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;
}
相关问题