title: Google Maps v3 set single marker point on map click
body: <p>Right now I have google map code that will set a single marker on a map.  What I want is for that single marker to be moved to whatever coordinates the user clicks on.  I only want 1 marker on the map, so I need that single marker to be moved to whatever location is clicked.  Any help is appreciated.  Thanks!</p>
  
  <pre><code>    var initialLocation;
      var siberia = new google.maps.LatLng(60, 105);
      var newyork = new google.maps.LatLng(40.69847032728747, -73.9514422416687);
      var browserSupportFlag =  new Boolean();
  
  
  
      function initialize() {
          var myOptions = {
              zoom: 6,
              mapTypeId: google.maps.MapTypeId.HYBRID
          };
          var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
          myListener = google.maps.event.addListener(map, 'click', function(event) {
              placeMarker(event.latLng);
              google.maps.event.removeListener(myListener);
          });
          google.maps.event.addListener(map, 'drag', function(event) {
              placeMarker(event.latLng);
              google.maps.event.removeListener(myListener);
          });
  
          // Try W3C Geolocation (Preferred)
          if(navigator.geolocation) {
              browserSupportFlag = true;
              navigator.geolocation.getCurrentPosition(function(position) {
                  initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
                  map.setCenter(initialLocation);
              }, function() {
                  handleNoGeolocation(browserSupportFlag);
              });
              // Try Google Gears Geolocation
          } else if (google.gears) {
              browserSupportFlag = true;
              var geo = google.gears.factory.create('beta.geolocation');
              geo.getCurrentPosition(function(position) {
                  initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
                  map.setCenter(initialLocation);
              }, function() {
                  handleNoGeoLocation(browserSupportFlag);
              });
              // Browser doesn't support Geolocation
          } else {
              browserSupportFlag = false;
              handleNoGeolocation(browserSupportFlag);
          }
  
          function handleNoGeolocation(errorFlag) {
              if (errorFlag === true) {
                  alert("Geolocation service failed.");
                  initialLocation = newyork;
              } else {
                  alert("Your browser doesn't support geolocation. We've placed you in Siberia.");
                  initialLocation = siberia;
              }
          }
  
          function placeMarker(location) {
              var marker = new google.maps.Marker({
                  position: location,
                  map: map,
                  draggable: true
              });
              map.setCenter(location);
              var markerPosition = marker.getPosition();
              populateInputs(markerPosition);
              google.maps.event.addListener(marker, "drag", function (mEvent) {
                  populateInputs(mEvent.latLng);
              });
          }
          function populateInputs(pos) {
              document.getElementById("t1").value=pos.lat()
              document.getElementById("t2").value=pos.lng();
          }
      }
  </code></pre>