Reputation: 546
How can I get the location that the user has clicked on the map via js?
Upvotes: 0
Views: 198
Reputation: 10191
Here is a callback function I used for a drop event on the map canvas once:
function placeDroppedMarker()
{
var locale = new google.maps.LatLng(mouseLat, mouseLng);
document.getElementById('map-lat').value = mouseLat;
document.getElementById('map-lng').value = mouseLng;
document.getElementById('map-icon-input').value = SelectedIcon;
var marker = new google.maps.Marker({
position: locale,
map: map,
draggable: true
});
var image = new google.maps.MarkerImage(
"http://www.twulogin.org/ourride/wp-content/themes/spectrum/images/" + SelectedIcon + ".png",
new google.maps.Size(37,37),
new google.maps.Point(0,0),
new google.maps.Point(16,37),
new google.maps.Size(37,37)
);
marker.setIcon(image);
map.setCenter(locale);
}
You have to set an event listener (in your map init function) like this to get mouseLat and mouseLng:
google.maps.event.addListener(map, 'mousemove', function(event) {
mouseLat = event.latLng.lat();
mouseLng = event.latLng.lng();
});
Where map is your google map.
The full working example is at: http://www.ourride.org/?page_id=238
Upvotes: 1