user821738
user821738

Reputation: 61

google maps API v3 set marker and get point

I have a problem to set marker and get latitude and longitude for that marker. How can I do that in google maps v3 API

var myLatlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
          zoom: 8,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
var map = new google.maps.Map(document.getElementById("divGoogleMaps"), myOptions);

google.maps.event.addListener(map, 'click', function() {

});

this is my start code.

Upvotes: 4

Views: 17728

Answers (2)

idrumgood
idrumgood

Reputation: 4924

var marker = new google.maps.Marker({
  position: myLatlng, 
  map: map, 
  title:"Hello World!"
}); 

Taken directly from Google Maps API v3

If you want to attach an event listener to the marker, it is advisable to do it right after you add it to the map, while you still have a fresh reference to it (in the case where you're potentially looping through some marker adding code, as is common).

Upvotes: 0

Nicolas
Nicolas

Reputation: 93

You should check Google Maps API doc web-site they have a fex example to help you started.

http://code.google.com/apis/maps/documentation/javascript/basics.html

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

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

Here you set a marker and get the position.

Upvotes: 4

Related Questions