如何使用JavaScript确定Google地图上特定位置的用户点击的当前坐标

基本上, 要获取用户在Google Maps实例上单击的位置的坐标, 你要做的就是将事件侦听器附加到地图本身。从接收到的事件中, 使用两个方法提取包含一个对象的latLng属性, 这两个方法是:lat和lng, 它们将分别返回纬度和经度值:

// Add click listener on the mapgoogle.maps.event.addListener(map, "click", function (event) {var latLng = event.latLng; // e.g Latitude: 7.135742728253103, Longitude: -73.11638207013567alert("Latitude: " + latLng.lat() + ", Longitude: " + latLng.lng()); });

完整文档中的以下实现显示了如何初始化Google Maps以及如何附加侦听器:
< !DOCTYPE html> < html lang="en"> < head> < meta charset="UTF-8"> < meta name="viewport" content="width=device-width, initial-scale=1.0"> < meta http-equiv="X-UA-Compatible" content="ie=edge"> < title> Google Maps Coordinates on Click< /title> < style> /* Always set the map height explicitly to define the size of the div* element that contains the map. */#map {width: 1200px; height: 800px; }/* Optional: Makes the sample page fill the window. */html, body {height: 100%; margin: 0; padding: 0; }< /style> < /head> < body> < !-- Google Maps Container --> < div id="map"> < /div> < script async defersrc="https://maps.googleapis.com/maps/api/js?key=YOUR_TOKEN& callback=initMap"> < /script> < script> var map; // Callaback executed when Google Maps script has been loadedfunction initMap() {map = new google.maps.Map(document.getElementById('map'), {zoom: 10, center: {lat: 7.118389341039206, lng: -73.12187523419817}}); // Add click listener on the mapgoogle.maps.event.addListener(map, "click", function (event) {var latLng = event.latLng; alert("Latitude: " + latLng.lat() + ", Longitude: " + latLng.lng()); }); }< /script> < /body> < /html>

【如何使用JavaScript确定Google地图上特定位置的用户点击的当前坐标】编码愉快!

    推荐阅读