简体   繁体   中英

Google Maps API check if user is close to a marker

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM