[英]Use google maps computeDistanceBetween to get the closest location returns NaN
[英]Google Maps API (3) computeDistanceBetween returns NaN
我們正在使用Google Maps API在我們的網站上顯示所有診所的列表,而我正在編寫“顯示我附近的所有診所”功能。 一切工作正常,使用幾何庫中的Google Maps Circle調用在地圖上繪制了一個圓。
我們要返回該圈子內所有診所的列表。 我們的診所通過$.get();
加載$.get();
從單獨的.js文件調用。 我一直在搞砸google.maps.geometry.spherical.computeDistanceBetween(pointA, pointB);
功能測試如果每個診所落在圓,其中內pointB
是圓的中心和pointA
是這兩者都是經由限定的clinic-的位置new google.maps.LatLng(clinic.lat, clinic.long));
。
ComputeDistanceBetween
會繼續為每個診所返回NaN
。 由於一些敏感性問題,我無法共享正在使用的確切代碼,但是我在這里修改了Google Maps API小提琴以進行標記聚類,因為我們還使用標記聚類,緯度/經度負載與我們類似。
我已經檢查了這篇文章 ,但對我來說沒有用。
您的代碼中有錯字。 locations[x].long
不存在,應該是locations[x].lng
因此您的功能應為:
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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.