从函数返回GMap位置对象

returning GMap location object from function

本文关键字:位置 对象 GMap 返回 函数      更新时间:2023-09-26

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

类似这样的东西:

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;
        }
    });
}

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

该函数由另一个进程调用,然后该进程将构建标记。

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

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

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

谢谢你的帮助。

基本代码位于:http://jsfiddle.net/2TXZ4/3/尽管不管出于什么原因地图都没画出来。

geocode方法是异步的,因此您需要在向该方法提供的回调中进行任何处理,或者您可以向codeAddress方法提供回调,就像下面描述的类似问题/答案一样(如何从Google Maps JavaScript地理编码回调返回变量?)。在下面的代码中,我将您在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);
}

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

好的,这里有一个疯狂的破解(确实有效):

 <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>

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