Reputation: 1406
I have to change the position of a marker on Google map from a javascript function. How can I achieve that?
Upvotes: 46
Views: 109320
Reputation: 2083
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);
}
Upvotes: 129
Reputation: 9533
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.
Upvotes: 2
Reputation: 1
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;
}
Upvotes: -9