I use the MapsActivity
class in this project. On my map you can see many markers. I have many known locations, but in my code I just display two locations for example.
I don't understand how to use the direction API and JSON. How can I display the route, distance, and travelling time from my current location (changing) to a known location (constant)?
public class MapsActivity extends FragmentActivity {
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private Map<Marker, Class<?>> allMarkersMap = new HashMap<Marker, Class<?>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
}
private void setUpMap() {
Marker marker1 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(14.608177, 120.967422))
.title("Sample2")
.snippet("zzzzzzz"));
allMarkersMap.put(marker1, MainActivity.class);
Marker marker2 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(14.611335, 120.962160))
.title("Sample1")
.snippet("sssssss"));
allMarkersMap.put(marker2, MainActivity2Activity.class);
mMap.setMyLocationEnabled(true);
mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Class<?> cls = allMarkersMap.get(marker);
Intent intent = new Intent(MapsActivity.this, cls);
startActivity(intent);
}
});
}
The code is for drawing the distance between any two points, start is the current location and the other is a location stored in the SQLite db.
}
This is the code for finding and drawing the routes between the points.
}
Have a look at this tutorial:
Drawing driving route directions between two locations using Google Directions in Google Map Android API V2
It shows how to draw a route map between two points, calculate distance and travel time.
If you are having problems in following the tutorial, download the Android Studio sample project from the link below:
MapDemo.zip