同步调用Google Geocoding API方法
当我更改标记位置时,我需要知道标记的地址。
基本上我有一个方法:
function addNewMarker(latLng) {
geocoder.geocode({'latLng': latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address;
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true,
title: address,
});
google.maps.event.addListener(marker, 'dragend', function() {
//marker.setTitle(getGeocodeResults(marker.getPosition()));
marker.setTitle(***THIS IS NEW ADDRESS OF THIS MARKER***);
});
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
如果我将地理编码代码提取到新方法中:
function addNewMarker(latLng){
var address = getGeocodeResults(latLng);
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true,
title: address,
});
google.maps.event.addListener(marker, 'dragend', function() {
marker.setTitle(getGeocodeResults(marker.getPosition()));
});
}
function getGeocodeResults(latLng){
geocoder.geocode({'latLng': latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address;
return address;
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
我没有运气,因为这个调用是异步的。当我停止移动标记时,我需要新的地址。 这个问题有解决办法吗?
I need to know marker's address when I change markers postition.
Basically I have a method:
function addNewMarker(latLng) {
geocoder.geocode({'latLng': latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address;
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true,
title: address,
});
google.maps.event.addListener(marker, 'dragend', function() {
//marker.setTitle(getGeocodeResults(marker.getPosition()));
marker.setTitle(***THIS IS NEW ADDRESS OF THIS MARKER***);
});
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
If I extract geocoding code into new method:
function addNewMarker(latLng){
var address = getGeocodeResults(latLng);
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true,
title: address,
});
google.maps.event.addListener(marker, 'dragend', function() {
marker.setTitle(getGeocodeResults(marker.getPosition()));
});
}
function getGeocodeResults(latLng){
geocoder.geocode({'latLng': latLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var address = results[0].formatted_address;
return address;
}
} else {
alert("Geocoder failed due to: " + status);
}
});
}
I have no luck because this call is asynchronous. And I need that new addres when I stop moving marker. Is there a solution to this one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原始发帖者的回答
[@vale4674 将此作为对他们问题的编辑。复制此处作为答案。]
@vale4674 替换
为
并添加了此方法:
ANSWER by Original Poster
[@vale4674 put this as an edit to their question. Copying here as an answer.]
@vale4674 replaced
with
and added this method: