Breaking code
Breaking code

Reputation: 75

how to draw path between 2 points on google map

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?

Upvotes: 0

Views: 37369

Answers (2)

vishal yadav
vishal yadav

Reputation: 1

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;
}

Upvotes: 0

Related Questions