I'd like users to be able to drop a pin on my custom Google Maps integration, similar to the ability to do this in the "My Maps" section on Google. I can't find anything about allowing a user to drop new pins through the API though.开发者_运维问答 Can anyone provide some info or point me to the right direction?
If you're using V3, a good example can be found at http://www.geocodezip.com/v3_example_click2add_infowindow.html. The tutorials at http://econym.org.uk/gmap/ are also a good place to start, and there are loads of useful links for V2 and V3 at http://www.geocodezip.com/.
While the other answers are helpful, I thought it good to add a simple code snippet for quick reference. Essentially you need to add an event listener to your map's init method, and within it, call new google.maps.Marker
. For example:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 41.763031, lng: -73.044465 },
zoom: 17.75
});
google.maps.event.addListener(map, 'click', function (event) {
new google.maps.Marker({
position: event.latLng,
map: map,
});
});
}
精彩评论