My map contains multiple points to tap the location of the user from where he passes, but the polyline is not shown on the roads, but shows a direct line from one marker to other. I want my polyline to go across the road: as the roads turns, it should also turn.
Here's my relevant code
dataholder = FirebaseDatabase.getInstance().getReference("UserLocation");
Log.d("onMapReady", "iam here");
dataholder.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("data",String.valueOf(dataSnapshot.getValue()));
System.out.println(dataSnapshot.getValue());
for (DataSnapshot a : dataSnapshot.getChildren()) {
MapData mapData = a.getValue(MapData.class);
arrayList.add(mapData);
lati.add(arrayList.get(i).getLatituide());
longit.add(arrayList.get(i).getLongitude());
Log.d("mapi","i am in loop");
mMap = googleMap;
Double lat = Double.parseDouble(lati.get(i));
Double longi = Double.parseDouble(longit.get(i));
sydney = new LatLng(longi, lat);
points.add(sydney);
MarkerOptions mop = new MarkerOptions();
mop.position(sydney);
mop.title("check");
mMap.addMarker(mop);
Log.d("latitude", String.valueOf(lat));
Log.d("longitude", String.valueOf(longi));
// Add a marker in Sydney and move the camera
mMap.addMarker(new MarkerOptions().position(sydney).title("check"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
i++;
}
line.addAll(points).width(5).color(Color.RED);
line.geodesic(true);
mMap.addPolyline(line);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getApplicationContext(),marker.getTitle().toString(),Toast.LENGTH_LONG).show();
return false;
}
});
Try to use Google Maps Roads API part Snap to Road which
Something like that for data from Google Maps Roads API Snap to Road example:
And don't forget to add Google Maps Roads API support for your project in Google APIs Console (select your project, than select Dashboard, than press "+ ENABLE APIS AND SERVICES " button, than on the left side "Filter by" select "Maps", than press "Google Maps Roads API" and, finally, press button "ENABLE").
You should get something like that:
where: blue polyline - for source points, red polyline - for snapped points.
Remember there is a limit of 100 GPS points and 2500 request per day per user (IP) and 10 requests per sec. And also you need more elegant solution for snapped points JSON downloading.