distanceBetween or other way to check if current position is in radius to marker

176 views Asked by At

I want to check if the current location of the user is in a certain radius to a marker. Should I do it with distanceBetween()? What arguments do I have to pass this function and what exactly does the result say?

LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
double latitude = myLocation.getLatitude();
double longitude = myLocation.getLongitude();

distanceBetween(???);

 public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results) {

}
2

There are 2 answers

0
fmt.Println.MKO On BEST ANSWER

you can simple use

distanceTo(Location location)

from Location class todo this, like:

Location makerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longtitude);
float meters = myLocation.distanceTo(markerLoc);
0
Adam Fręśko On

Please use Google Maps Utility Library

This library provide very simple distance calculating just like you need

In example:

computeDistanceBetween() – Returns the distance, in meters, between two 
latitude/longitude coordinates.

computeHeading() – Returns the bearing, in degrees, between two 
latitude/longitude coordinates.

computeArea() – Returns the area, in square meters, of a closed path on the 
Earth.

interpolate() – Returns the latitude/longitude coordinates of a point that 
lies a given fraction of the distance between two given points. You can use 
this to animate a marker between two points, for example.