如何解决在地理编码功能之外获取标记
| 我希望能够在地址解析功能之外捕获标记对象,并且无法弄清楚该怎么做。 请帮我。 代码: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中不会有任何用处。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。