获取在谷歌地图上点击的任何点的坐标

Get the coordinates of any point clicked on a Google map

本文关键字:任何点 坐标 谷歌地图 获取      更新时间:2023-09-26

我有一个固定大小的<div>,我正在通过中心坐标(22.308259,73.180293)绘制谷歌地图。

当我单击地图上的任意位置时,我会收到一个警报,其中包含我指定为中心坐标的坐标

以下是我使用的脚本

<script type="text/javascript">
    window.onload = function () {
        var mapOptions = {
            center: new google.maps.LatLng(22.308259, 73.180293),
            zoom: 14,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var infoWindow = new google.maps.InfoWindow();
        var latlngbounds = new google.maps.LatLngBounds();
        var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
        google.maps.event.addListener(map, 'click', function (e) {
            alert("Latitude: " + e.latLng.lat() + "'r'nLongitude: " + e.latLng.lng());
        });
    }
</script>

我希望无论我在地图上单击何处,我都会得到在地图上单击的点的坐标,而不是到目前为止我得到的中心坐标。我的代码需要更改哪些内容?

function setupListener(map, name) {
        google.maps.event.addListener(map, name, function(e) {
            if (name == "click") {
                alert(e.latLng.lat().toFixed(6));
            }
        });
      }

找不到您的错误,但这有效。希望它有帮助。