好的,我在测试网站上设置了地图和自动填充字段。
我的目标是让用户在字段中输入他/她的地址,然后当他/她点击确认根据提供的信息确定离他们最近的商店时。
我对每个商店都有lat / long,并且在自动完成的帮助下,我现在有了lat / long的用户地址,但是当我使用文档中提供的computeDistanceBetween方法时,我得到NaN而不是期望的结果。< / p>
以下是指向测试网站的链接,以获取相关信息 - http://dev.touch-akl.com/map/
以下是我到目前为止所尝试的内容
//----------------------------------------------------------------
//--- 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));
});
答案 0 :(得分:29)
google.maps.geometry.spherical.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));
});