-1

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.

Community
  • 1
  • 1
Dan Kaufman
  • 115
  • 1
  • 13

1 Answers1

2

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

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Oh G-d D--- it! Always something simple. I've gone over this 100 times now and I apparently missed that everytime. Let me try it out in my dev code and see how it shakes out. – Dan Kaufman Jan 06 '17 at 16:56
  • That was it but in the case of dev code I had `long` instead of `lng`. Oy. Thanks! – Dan Kaufman Jan 06 '17 at 16:59