简体   繁体   English

Google Maps API(3)computeDistanceBetween返回NaN

[英]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. 我们正在使用Google Maps API在我们的网站上显示所有诊所的列表,而我正在编写“显示我附近的所有诊所”功能。 Everything is working fine and I have a circle being drawn on the map using the Google Maps Circle call from the geometry library. 一切工作正常,使用几何库中的Google Maps Circle调用在地图上绘制了一个圆。

We want to return a list of all clinics that fall within that circle. 我们要返回该圈子内所有诊所的列表。 Our clinics are loaded via a $.get(); 我们的诊所通过$.get();加载$.get(); call from a separate .js file. 从单独的.js文件调用。 I've been messing with the google.maps.geometry.spherical.computeDistanceBetween(pointA, pointB); 我一直在搞砸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)); 功能测试如果每个诊所落在圆,其中内pointB是圆的中心和pointA是这两者都是经由限定的clinic-的位置new google.maps.LatLng(clinic.lat, clinic.long)); .

ComputeDistanceBetween keeps returning NaN for every clinic. ComputeDistanceBetween会继续为每个诊所返回NaN 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. 由于一些敏感性问题,我无法共享正在使用的确切代码,但是我在这里修改了Google Maps API小提琴以进行标记聚类,因为我们还使用标记聚类,纬度/经度负载与我们类似。

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 locations[x].long不存在,应该是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 概念证明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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