0

I am trying to make an application on Android which allows me to see the distance between two points on google maps, the distance points being The location i am currently at and the other point being a marker on the google maps.

I have managed to set where I am on the map using setMyLocationEnabled(true) and i want to then be able to compare it to a marker i have on the google map which is

map.addMarker(newMarkerOptions().position(LOCATION_BEIJING).title("Find me here!"));
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
Oscurra
  • 11
  • 1
  • 5

1 Answers1

1

You can use the Location class here. First set the marker location:

Location markerLoc = new Location("Marker");
markerLoc.setLatitude(marker.latitude);
markerLoc.setLongitude(marker.longitude);

See this to get the current Location using OnMyLocationChangeListener and set your Current Location:

Location currentLoc = new Location("Current");
currentLoc.setLatitude(location.latitude);
currentLoc.setLongitude(location.longitude);

Then you can use the Location class' distanceTo method to get the distance in meters like this:

Float distance = currentLoc.distanceTo(markerLoc);
Community
  • 1
  • 1
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41