【发布时间】:2015-03-25 08:24:34
【问题描述】:
我有以下脚本,旨在将变量从 Google Maps API v3 地理编码的纬度和经度传递到半正弦公式。但是,每次它说我的地理编码变量“未定义”:
var address = ['London'];
jQuery.each(address, function(index, item) {
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': item}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
startlat = results[0].geometry.location.lat();
startlng = results[0].geometry.location.lng();
}
});
});
// start of haversine for marker distances
Number.prototype.toRad = function() {
return this * Math.PI / 180;
};
var mlat = '50.1';
var mlng = '-1.05';
var RDis = '3963'; // miles; Change to 6371 for km
var x1 = mlat - startlat;
var dLat = x1.toRad();
var x2 = mlng - startlng;
var dLon = x2.toRad();
var aDis = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(startlat.toRad()) * Math.cos(mlat.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var cDis = 2 * Math.atan2(Math.sqrt(aDis), Math.sqrt(1-aDis));
var dDis = RDis * cDis;
// dDis is the distance
谁能建议我如何通过脚本传递这些信息?将地理编码封装到与 hasrsine 相同的函数中不是一种选择(我也需要在其他地方使用它们中的每一个)。
感谢您的帮助。 :)
【问题讨论】:
标签: javascript google-maps-api-3 geocoding var