0

我希望能够在地理编码功能之外捕获标记对象并且无法弄清楚如何。
请帮我。

编码:

geocoder.geocode({ address: address }, 
    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK && results.length) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map
                });
            }
        }
    });

提前致谢!

4

1 回答 1

1

只需marker在回调之外声明您的变量并在回调中为其分配一个值:

var marker = null;
geocoder.geocode({ address: address }, 
    function(results, status) {
        if (status == google.maps.GeocoderStatus.OK && results.length) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                map.setCenter(results[0].geometry.location);
                marker = new google.maps.Marker({
                    position: results[0].geometry.location,
                    map: map
                });
            }
        }
    }
);

marker但是要小心,回调是异步的,所以在回调被触发之前你不会有任何有用的东西。

于 2011-05-31T18:22:00.523 回答