【问题标题】:returning GMap location object from function从函数返回 GMap 位置对象
【发布时间】:2011-09-19 19:16:24
【问题描述】:

我正在尝试为地图工具做一个简单的地理编码功能。我的地理编码很好,但我希望将位置对象作为地理编码函数的返回值传回。

类似这样的:

function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            console.dir(location);
            return location;
        } else {
            return false;
        }
    });
}

位置项的console.dir显示了预期的位置对象,因此该函数正在被调用并成功返回数据。

这个函数被另一个进程调用,然后它会构建标记。

if (coordinates = codeAddress(myaddress){
    // do stuff
}

但是,coordinates 变量的计算结果始终为未定义,因此永远不会满足“做事”的条件。

我知道我可能遗漏了关于坐标 var 定义的一些非常明显的内容,但我不确定它是什么。

感谢您的帮助。

基本代码位于:http://jsfiddle.net/2TXZ4/3/,但无论出于何种原因,地图都没有绘制出来。

【问题讨论】:

  • 好的,在我看来,问题在于这是一个异步请求——如果我在 codeAddress 函数中硬编码返回值,它就会按预期工作。所以它必须只是请求的性质。然而,仍然不确定如何最好地处理这个问题。

标签: javascript geolocation google-maps-api-3


【解决方案1】:

geocode 方法是异步的,因此您需要在提供给该方法的回调中进行任何处理,或者您可以像这个类似的问题/答案描述的那样为您的 codeAddress 方法提供回调 (How do I return a variable from Google Maps JavaScript geocoder callback?) .在下面的代码中,我将您在 doStuff 函数中所做的事情移到了 geocode 函数的回调中 - 它应该可以工作,但我无法使用您提供的 jfiddle 链接对其进行测试(可能是由于 Google Maps API 密钥)。我希望这会有所帮助!

function codeAddress(address) {
    geocoder.geocode({
        'address': address
    }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var location = results[0].geometry.location;
            console.log(location);
            map.setCenter(location);
            var marker = new google.maps.Marker({
                map: map,
                position: location
            });
        } else {
            alert("Geocode was not successful for " + address);
        }
    });
}


window.doStuff = function() {
    var address = document.getElementById("address").value;
    codeAddress(address);
}

【讨论】:

  • 这是正确的,我最终做了什么。我仍然在“doStuff”函数中创建了标记,但随后在 codeAddress 回调中使用了 marker.setPosition(location) 方法来设置位置。
【解决方案2】:

if (coordinates = codeAddress(myaddress) - 这是一个分配,而不是比较。

好的,这是一个疯狂的 hack(确实有效):

 <script type="text/javascript"> var geocoder; var loc; var l = false;  
 function  initialize() {

 geocoder = new google.maps.Geocoder();

 var mapDiv = document.getElementById('map_canvas');
     map = new google.maps.Map(mapDiv, {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8,
       mapTypeId: google.maps.MapTypeId.ROADMAP
    });      }


function codeAddress(address) {
geocoder.geocode({
    'address': address
}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        loc = results[0].geometry.location;             l = true;
        console.log(loc);           //alert (loc);
        //return loc;       doStuff(loc)
    } else {
        return false;
    }
}); }


 function doStuff(geo_marker){ if(l==false){
var thisaddress = document.getElementById("address").value;
var myResult = codeAddress(thisaddress); }else{

if (loc) {
    map.setCenter(loc);
    var marker = new google.maps.Marker({
        map: map,
        position: loc
    });
 l = false;  //important to use function more than once :))
} else {
    alert("Geocode was not successful");
}   }

};

google.maps.event.addDomListener(window, 'load', initialize); 
</script>



 <body>
 <div>
 <input id="address" type="textbox" value="Sydney, NSW">
 <input type="button" value="Geocode" onclick="window.doStuff()">
 </div>
 <div id="map_canvas" style="height:600px; width: 500px; position: absolute; 
  top: 80px">    </div> 

 </body>

我确实认为在一个函数中进行地理编码和向地图添加标记更有意义,以避免在函数之间来回调用。

【讨论】:

  • @AR-- 我也尝试了以下方法,结果相同(ccord 未定义):var coord = codeAddress(tempaddress); console.dir(coord); if (coord){ //dostuff }
  • 我已经更新了我的答案。该示例确实有效,您只需要根据需要对其进行更新。
猜你喜欢
  • 2012-02-29
  • 1970-01-01
  • 2021-08-09
  • 2016-03-17
  • 2019-10-13
  • 2013-01-13
  • 2015-12-15
  • 2011-02-03
  • 2020-06-28
相关资源
最近更新 更多