Google地图v3可拖动标记 [英] Google maps v3 draggable marker
本文介绍了Google地图v3可拖动标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
marker = new google.maps.Marker(
{
map :map,
draggable:true,
animation:google.maps.Animation.DROP,
position:results [0] .geometry.location
});
这是我的标记位置,当我初始化标记位置时,我知道地名(例如:XY街道,纽约),但由于可拖动的选项它正在改变,我的问题是如何获得新的地名,我需要什么事件处理程序。
解决方案
最后我找到了答案:
marker = new google.maps.Marker(
{
map:map,
draggable:true,
animation:google。 maps.Animation.DROP,
position:results [0] .geometry.location
});
google.maps.event.addListener(marker,'dragend',function()
{
geocodePosition(marker.getPosition());
});
函数geocodePosition(pos)
{
geocoder = new google.maps.Geocoder();
geocoder.geocode
({
latLng:pos
},
函数(结果,状态)
{
if(status == google.maps.GeocoderStatus.OK)
{
$(#mapSearchInput).val(results [0] .formatted_address);
$(#mapErrorMsg)。hide(100 );
}
else
{
$(#mapErrorMsg)。html('Can not determine address at this location。'+ status).show(100);
}
}
);
}
I'm new in google maps, and I'm trying to learn it.
marker = new google.maps.Marker(
{
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location
});
This is my marker position, when I'm initialising the marker position than I know the place name (for example: XY street, New York,), but because of the draggable option it is changing, and my question is how can I get the new place name, what event handler do I need.
解决方案
Finally I found the answer:
marker = new google.maps.Marker(
{
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location
});
google.maps.event.addListener(marker, 'dragend', function()
{
geocodePosition(marker.getPosition());
});
function geocodePosition(pos)
{
geocoder = new google.maps.Geocoder();
geocoder.geocode
({
latLng: pos
},
function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
$("#mapSearchInput").val(results[0].formatted_address);
$("#mapErrorMsg").hide(100);
}
else
{
$("#mapErrorMsg").html('Cannot determine address at this location.'+status).show(100);
}
}
);
}
这篇关于Google地图v3可拖动标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文