I have a app that opens the map and zooms on my location with a marker and then if i longClick on any place it saves that location in database everything works fine but one thing i cannot manage is draw a line of path between two location markers.
these are three global variables
Location myLocation;
LatLng onmaplongclicklastlatlng;
private GoogleMap mMap;
mylocation
is assigned by LocationListener
and onmaplongclicklastlatlng
is assigned when i long click on any place on the map.
placing the marker on both location is done i already have the two points i want to draw the line on.
i just need to draw a line between those two locations (my current location periodically updated in the myLocation variable)
and onmaplongclicklastlatlng changed whenever i long click on the map.
the whole code is done and i have a button
that i want to use to draw
the line
since i can access the two location variables
from this button
method, Other code need not be changed.
public void getLine(View view) {
// need to draw a line here from user gps posiution to the marker location
}
i have tried many tutorials but they do not work for some reason, any help would be really appreciated.
oh and last but not least
onmaplongclicklastlatlng
is a LatLng and
myLocation
is a Location
but they can be interchanged to fit the purpose
ex. Location maplocation = new Location(onmaplongclicklastlatlng
.latitude, onmaplongclicklastlatlng.longitude);
and
Latlng myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
As i said any help would be really appreciated. And let me know if anything else needed i will edit the question then.
To calculate accurate distance between two loaction you have to use Google Map Distance Matrix API. Construct your url like below.
https://maps.googleapis.com/maps/api/distancematrix/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
And to draw line between two location you have to first use the Google Map Direction Api, construct your url like below
https://maps.googleapis.com/maps/api/directions/json?" +"origin=" + origin.latitude + "," + origin.longitude+ "&" +"destination=" + dest.latitude + "," + dest.longitude +"&sensor=false&units=metric&mode=driving" + "&" + key=YOUR_API_KEY
above api call return a list of latlng add the list into polylines
Polyline polyline1 = googleMap.addPolyline(new PolylineOptions()
.clickable(true)
.addAll(latlngList));
hope it helps.