I am using google map api v2 in my android application, I am able to show the map and put markers on it, but now I am stuck with the problem in which I need to find out the distance between two markers or points placed on map, I have already gone through the docs but didn't find anything helpful in this case.
If anyone knows how to solve this then please help me.
Thanks
In
Google Map API V2
You haveLatLng
objects so you can't usedistanceTo
(yet).You can then use the following code considering oldPosition and newPosition are
LatLng
objects :For more informations about the
Location
class see this linkThis is an old question with old answers. I want to highlight an updated way of calculating distance between two points. By now we should be familiar with the utility class, "SphericalUtil". You can retrieve the distance using that.
All this answers will give you the distance in a straight line. If you need to get the distance by road, you have to parse the JSON that Google sends you back after calling his service. You can use this method:
lat1 and lon1 are coordinates of origin, lat2 and lon2 are coordinates of destination.
@salman khan what Usman Kurd suggested is perfect. Only thing i found which can be corrected is that "For google maps v2 we use LatLng class. So below is the code of Usman Kurd that can be used for Google Maps v2. I checked it works perfect.
You should use the android Location
You can do:
And also:
And you will get the distance in meters between location1 and location2 in meters. And beetween latLongA ant latLongB.
Using location.
//Main Activity