【问题标题】:Error passing variable to javascript formula ("undefined") [duplicate]将变量传递给javascript公式时出错(“未定义”)[重复]
【发布时间】: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


    【解决方案1】:

    地图 API 调用是异步的,所以在回调发生之前你不能使用startlatstartlng

    // ****** Moved this
    // start of haversine for marker distances
    Number.prototype.toRad = function() {
        return this * Math.PI / 180;
    };
    
    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();
    
                // ********* Moved block starts here *********
                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
                // ********* Moved block ends here *********
            }
        });
    });
    

    更多信息在How to return the response from an asynchronous call?

    【讨论】:

    • 感谢您这么快的回复,@T.J.克劳德。这是否意味着它每次都调用地图 API 调用?变量mlatmlng 来自一个数组(脚本的不同部分),因此我需要能够将startlatstartlng 存储在可以重复引用而无需创建每次唯一的呼叫。有意义吗?
    • @user1259798:您可以存储它们并重复使用它们,但在回调发生之前您不能第一次使用它们,因为在此之前您还没有它们。
    • 非常感谢您的回答,也很抱歉在此问题上如此n00b。我很难理解如何调用某些东西,然后再引用它?你能解释一下我将如何调用我的第一个函数,然后在后面的函数中引用由该调用填充的变量吗?很抱歉需要这么多的教育——我对这些概念理解得不够好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多