Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. so I wanted to put this feature to my app.

Something like that.

enter image description here

with live update when the user move.

Anyhow this my app

enter image description here

I don't know how to start on that placing DIRECTION. anyone can help me?

Tried using this method but i failed.

` @Override public void onLocationChanged(Location location) { // Getting latitude of the current location double latitude = location.getLatitude();

    // Getting longitude of the current location
    double longitude = location.getLongitude();

    // Creating a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);

    // Showing the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(18));



    start = latLng;

    Log.d("Location Changed: ", latLng.toString());
    //etLog.append("Location Changed: " + latLng.toString() + System.getProperty("line.separator"));
}

`

like image 453
Pseudorandom Avatar asked Aug 25 '26 13:08

Pseudorandom


1 Answers

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

public class MapActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,LocationListener{ 
final String TAG = "mapactivity";
LocationRequest  locationRequest;
GoogleAPiClient googleApiClient;
googleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).build();
@Override
public void onStart(){
    super.onStart();
    googleApiClient.connect();
}
@Override
public void onStop(){
    googleApiClient.disconnect();
    super.onStop();
}
@Override
public void onConnectionSuspended(int i){
    Log.i(TAG, "Connection suspended");

}
@Override
public void onConnectionFailed(ConnectionResult connectionResult){
    Log.i(TAG, "connection failed");

}
@Override
public void onConnected(Bundle bundle){
    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(1000);
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
@Override
public void onLocationChanged(Location location){
    Double curLat = location.getLatitude();//current latitude
    Double curLong = location.getLongitude();//current longitude
} }
like image 55
Prathap Badavath Avatar answered Aug 28 '26 02:08

Prathap Badavath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!