Google maps save marker location?

妖精的绣舞 提交于 2019-12-06 11:15:37

By save do you mean persist to local storage?

You could modify the dragend event listener so that it updates the local storage with the latitude and longitude of the position the marker was dragged to. As this event is triggered whenever the marker is dragged, the local storage will always contain the location of the last drag (i.e. if the user drags the marker 4 times, local storage will hold the position of the fourth drag).

google.maps.event.addListener (marker, 'dragend', function (event) 
{
    var point = marker.getPosition();
    map.panTo(point);

    // save location to local storage
    localStorage['lastLat'] = point.lat();
    localStorage['lastLng'] = point.lng();
}); 

Obviously this would only work in browsers that have support for local storage so it would be wise to check for this before attempting to access the local storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!