问题
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.
with live update when the user move.
Anyhow this my app
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"));
}
`
回答1:
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
} }
回答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.
来源:https://stackoverflow.com/questions/34304279/android-onlocationchanged-with-direction-using-google-map-api-v2