问题
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