简体   繁体   中英

Google Maps API (3) computeDistanceBetween returns NaN

We're using Google Maps API to show a list of all our clinics on our website and I'm in the process of writing a 'Show all clinics near me' feature. Everything is working fine and I have a circle being drawn on the map using the Google Maps Circle call from the geometry library.

We want to return a list of all clinics that fall within that circle. Our clinics are loaded via a $.get(); call from a separate .js file. I've been messing with the google.maps.geometry.spherical.computeDistanceBetween(pointA, pointB); function to test if each clinic falls within the circle, where pointB is the center of the circle and pointA is the position of the clinic- both of which are being defined via new google.maps.LatLng(clinic.lat, clinic.long)); .

ComputeDistanceBetween keeps returning NaN for every clinic. Due to some sensitivity issues I cannot share the exact code I'm working with but I modified a Google Maps API fiddle for marker clustering HERE because we're also using marker clustering and the lat/longs load similarly to ours.

I already checked this post and it didn't work out for me.

You have a typo in your code. locations[x].long doesn't exist, that should be be locations[x].lng

so your function should be:

  var mylocation = new google.maps.LatLng(locations[0].lat, locations[0].lng);
  console.log(mylocation);
  var marker_lat_lng = new google.maps.LatLng(locations[2].lat, locations[2].lng);
  console.log(marker_lat_lng);
  var distance_from_location =  google.maps.geometry.spherical.computeDistanceBetween(mylocation, marker_lat_lng);
  document.getElementById('feedback').innerHTML = distance_from_location;

proof of concept fiddle

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