jQuery 使用谷歌地图 computeDistanceBetween 获取最近的位置返回 NaN
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18822948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use google maps computeDistanceBetween to get the closest location returns NaN
提问by Chris G-Jones
Ok so I have set up a map and an autocomplete field on a testing site.
好的,我已经在测试站点上设置了地图和自动完成字段。
My goal is to get the user to input his/her address into the field and then when he/she clicks confirm work out the closest store to them based on the information provided.
我的目标是让用户在字段中输入他/她的地址,然后当他/她点击确认时,根据提供的信息确定离他们最近的商店。
I have the lat/long for each store and with the help of autocomplete I now have the lat/long of the users address but when I use the computeDistanceBetween method provided in the documentation I get NaN instead of a desired result.
我有每个商店的经纬度,在自动完成的帮助下,我现在有了用户地址的经纬度,但是当我使用文档中提供的 computeDistanceBetween 方法时,我得到 NaN 而不是所需的结果。
Here is a link to the test site to get an idea - http://dev.touch-akl.com/map/
这是测试站点的链接以获取想法 - http://dev.touch-akl.com/map/
And Below is what I have tried so far
以下是我迄今为止尝试过的
//----------------------------------------------------------------
//--- Search Box -------------------------------------------------
var input = document.getElementById('address');
var $confirm = $('#confirm');
var options = {
types: ['geocode'],
componentRestrictions: {country: 'nz'}//Turkey only
};
var autocomplete = new google.maps.places.Autocomplete(input,options);
autocomplete.bindTo('bounds', map);
$confirm.click(function(){
var _street = $('#address').val();
var place = autocomplete.getPlace();
var _coordinates = place.geometry.location.toString();
var _delivery = _coordinates.substring(1, _coordinates.length - 1);
var _kCord = '-36.874694,174.735292';
var _pCord = '-36.858317,174.782284'
console.log(_coordinates);
console.log(_delivery);
console.log(google.maps.geometry.spherical.computeDistanceBetween(_pCord, _delivery));
console.log(google.maps.geometry.spherical.computeDistanceBetween(_kCord, _delivery));
});
回答by astupidname
google.maps.geometry.spherical.computeDistanceBetween requires that you feed it two google.maps.LatLng objects whereas you are using strings, and that won't work. The following should...
google.maps.geometry.sphereal.computeDistanceBetween 要求您在使用字符串时为其提供两个 google.maps.LatLng 对象,但这是行不通的。以下应...
$confirm.click(function(){
var _street = $('#address').val();
var place = autocomplete.getPlace();
var _coordinates = place.geometry.location; //a google.maps.LatLng object
var _kCord = new google.maps.LatLng(-36.874694, 174.735292);
var _pCord = new google.maps.LatLng(-36.858317, 174.782284);
console.log(_coordinates);
console.log(google.maps.geometry.spherical.computeDistanceBetween(_pCord, _coordinates));
console.log(google.maps.geometry.spherical.computeDistanceBetween(_kCord, _coordinates));
});