javascript – 从Google地图获取点击事件的经纬度

我已成功实施Google地图.但还有一件事要做.当用户点击地图(任何坐标)时,我需要检索经度和纬度数据.我的整个代码看起来像这样:`

<script type="text/javascript">
  var map;
  function initialize() {
      var myLatlng = new google.maps.LatLng(<?=$decimalValueLon?>,<?=$decimalValueLat?>);
      var myOptions = {
      zoom: 17,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };


    map = new google.maps.Map(document.getElementById('map_canvas'),
        myOptions);

    // marker STARTS    
    var marker = new google.maps.Marker({
    position: myLatlng, 
    title:"Click to view info!"
});  
    marker.setMap(map);
    // marker ENDS

    // info-window STARTS   
    var infowindow = new google.maps.InfoWindow({ content: "<div class='map_bg_logo'><span style='color:#1270a2;'><b><?=$row->bridge_name?></b> (<?=$row->bridge_no?>)</span><div style='border-top:1px dotted #ccc; height:1px;  margin:5px 0;'></div><span style='color:#555;font-size:11px;'><b>Length: </b><?=$row->bridge_length?> meters</span></div>" });
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
    // info-window ENDS 
 }

  google.maps.event.addDomListener(window, 'load', initialize);
</script>` 

提前致谢 !

你可以添加一个点击处理程序,就像你有一个加载处理程序:

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

    //lat and lng is available in e object
    var latLng = e.latLng;

});
翻译自:https://stackoverflow.com/questions/9247006/get-latitude-and-longitude-on-click-event-from-google-map

转载注明原文:javascript – 从Google地图获取点击事件的经纬度