在地理编码功能之外获取标记

问题描述

| 我希望能够在地址解析功能之外捕获标记对象,并且无法弄清楚该怎么做。 请帮我。 代码
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
                });
            }
        }
    });
提前致谢!     

解决方法

        只需在回调外部声明
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
                });
            }
        }
    }
);
但是请注意,回调是异步的,因此在触发回调之前,在ѭ1中不会有任何用处。