Skip to content Skip to sidebar Skip to footer

Android Onlocationchanged With Direction Using Google Map Api V2

I am making a tourist map for android with Google Map Markers and Polylines and I succeeded in doing that but now i need to add something that can make my app more friendly user. s

Solution 1:

Try this code you will get updated location live on map.

publicclassMapActivityextendsAppCompatActivityimplementsGoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,LocationListener{ 
final StringTAG = "mapactivity";
LocationRequest  locationRequest;
GoogleAPiClient googleApiClient;
googleApiClient = newGoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
@OverridepublicvoidonStart(){
    super.onStart();
    googleApiClient.connect();
}
@OverridepublicvoidonStop(){
    googleApiClient.disconnect();
    super.onStop();
}
@OverridepublicvoidonConnectionSuspended(int i){
    Log.i(TAG, "Connection suspended");

}
@OverridepublicvoidonConnectionFailed(ConnectionResult connectionResult){
    Log.i(TAG, "connection failed");

}
@OverridepublicvoidonConnected(Bundle bundle){
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(1000);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@OverridepublicvoidonLocationChanged(Location location){
    Double curLat = location.getLatitude();//current latitudeDouble curLong = location.getLongitude();//current longitude
} }

Solution 2:

You have to enable google map directions api from google developer console. To get directions you have to pass two latlngs as source and destination with your api key you will get all the points between given source and destination draw the polyline between them you will get directions polyline drawn between source and destination as shown in screenshot follow this link.

Post a Comment for "Android Onlocationchanged With Direction Using Google Map Api V2"