且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Maps api V3 更新标记

更新时间:2023-02-14 13:08:18

更新

此外,您的全局 map 引用永远不会设置为实际地图实例,因为您使用本地 var 相同名称对其进行了阴影处理.

Also, your global map reference is never set to the actual map instance since you shadow it with a local var same name.

var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

这应该只是

map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

您在初始化之前使用 latlng 作为标记位置(除非它们在某处全局设置):


You're using lat and lng for the marker position before they're initialized (unless they're globally set somewhere):

var newLatLng = new google.maps.LatLng(lat, lng);
marker.setPosition(newLatLng);

如果你想更新同一个标记的位置而不是创建一个新的,你应该这样做:

If you want to update the position of the same marker and not create a new one, you should simply be doing this:

$("#updateMap").click(function(){
    var lat = parseFloat(document.getElementById('markerLat').value);
    var lng = parseFloat(document.getElementById('markerLng').value);
    var newLatLng = new google.maps.LatLng(lat, lng);
    marker.setPosition(newLatLng);
});