Javascript – Allowing users to add markers to google maps and getting the coordinates

google mapsgoogle-maps-api-3javascript

What i want to do is embed a map on my website and allow users to place markers on it (also if there is a way to control how many markers a user can put on the map?) and i also want to get the coordinates of these markers once they have been put on the map. From the documentation that i've read for google maps javascript api V3, i can place markers on the map myself, but i dont see a way to let users put them up on the map. Is there a way to do it?

Best Answer

Source: http://code.google.com/apis/maps/documentation/javascript/events.html Scroll down to accessing arguments in UI events.

var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
  var myOptions = {
    zoom: 4,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  google.maps.event.addListener(map, 'click', function(event) {
    placeMarker(event.latLng);
  });
}

function placeMarker(location) {
  var marker = new google.maps.Marker({
      position: location, 
      map: map
  });

  map.setCenter(location);
}