How to change the position of marker from a javascript function?

混江龙づ霸主 提交于 2019-12-18 10:54:31

问题


I have to change the position of a marker on Google map from a javascript function. How can I achieve that?


回答1:


You can use setPosition function of the marker class

function changeMarkerPosition(marker) {
    var latlng = new google.maps.LatLng(-24.397, 140.644);
    marker.setPosition(latlng);
}



回答2:


First off you must store the marker in an array when you create it so you can have access to it afterwards.
Then change the position with marker.setPosition() as solidrevolution mentioned.




回答3:


Try this:

var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude +
        "<br>Longitude: " + position.coords.longitude;
}


来源:https://stackoverflow.com/questions/5818129/how-to-change-the-position-of-marker-from-a-javascript-function

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