4

I have a json file with plenty GPS coordinates (lat,lng) which will be displayed as marker in Google Maps. Now I want to check whether the user is close to a marker - let's say 10 meters.

My approach was to simply compare the users current location (lat/lng) with my json array. For this I would have to calculate the actual radius for each marker which I find quite difficult to map to lat/lng.

Anyways, I'm not that familiar with Google Maps API and couldn't figure out myself whether there is already some function for this.

Thanks, Dennis

Dennis
  • 53
  • 3
  • Does your JSON contain lat and long of your markers? – John May 11 '17 at 12:16
  • Possible duplicate of [Calculate distance between two latitude-longitude points? (Haversine formula)](http://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula) – John May 11 '17 at 12:21
  • related question: [How to write a conditional to detect pegman location and create popup box in GoogleMaps?](http://stackoverflow.com/questions/38776046/how-to-write-a-conditional-to-detect-pegman-location-and-create-popup-box-in-goo) – geocodezip May 11 '17 at 12:54

1 Answers1

4

You can create function like this and find out distance.

function getDistance(source, destination) {
  return google.maps.geometry.spherical.computeDistanceBetween(
    new google.maps.LatLng(source.lat, source.lng),
    new google.maps.LatLng(destination.lat, destination.lng)
  );
}

With distance you can write your further logic. Hope this will help.