开发者

Google Maps Javascript API V3 - Want to add a marker and return the LatLng of the marker

开发者 https://www.devze.com 2023-03-23 10:39 出处:网络
I have a question relevant to Google Maps API. I am learning how to use the Google Maps API. I can just show the 开发者_Python百科map in the div tag, but what I want is to be able to click on the map

I have a question relevant to Google Maps API. I am learning how to use the Google Maps API. I can just show the 开发者_Python百科map in the div tag, but what I want is to be able to click on the map and show the marker and return the LatLng of the clicked point.

function initialize(v_lat,v_long,v_place) {
   var latlng = new google.maps.LatLng(-34.397, 150.644);
   var myOptions = {
         zoom: 15,
         center: latlng,
         mapTypeId: google.maps.MapTypeId.SATELLITE
   };

   var map = new google.maps.Map(   document.getElementById("map_canvas")       ,   myOptions   );
}


here is the click event example

where you click on map marker place on that point and click on marker which will return the latlng of that location.

try this

var marker;
google.maps.event.addListener(map, 'click', function() {
   if(marker==null){
       marker = new google.maps.Marker({
          position: myLatlng, 
          map: map,
          title:"Hello World!"
       });
       google.maps.event.addListener(marker, 'click', function() {
             alert("latlng" + marker.getPosition());
       });

   }
});

here is the link for google map v3 you can find all tutorials related to the map

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消