通过谷歌地图的点击事件获取谷歌纵横和经度 [英] Get Latitude and Longitude on click event from Google map

查看:96
本文介绍了通过谷歌地图的点击事件获取谷歌纵横和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功实施了Google地图。但还有一件事要做。当用户点击地图时(任何坐标),我需要检索经度和纬度数据。我的整个代码如下所示:

I have implemented the Google map successfully. But one more thing has left to do. I need to retrieve the Longitude and Latitude data when the user clicks on the map (any co-ordinates). My entire code looks like this:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<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>` 

感谢您!

推荐答案

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

You can add a click-handler like you have a load handler:

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

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

});

这篇关于通过谷歌地图的点击事件获取谷歌纵横和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆