Google地图API 3:从右键单击获取坐标

我有2个文本框经纬度为用户正在input一个新的logging到分贝。

我有一个很棒的谷歌地图的实时预览,现在我想要做的就是在地图上添加一个右键点击事件,单击坐标来填充经纬度文本框。

这甚至有可能吗?

我知道如何添加事件监听器,并查看API文档,但没有看到任何这样做。 我知道你可以在谷歌的地图上做到这一点。

google.maps.event.addListener(map, "rightclick", function(event) { var lat = event.latLng.lat(); var lng = event.latLng.lng(); // populate yor box/field with lat, lng alert("Lat=" + lat + "; Lng=" + lng); }); 

您可以创build一个InfoWindow对象( 这里是类文档 ),并附加一个rightclick事件处理程序,该处理程序将用地图上点击位置的纬度和经度填充它。

 function initMap() { var myOptions = { zoom: 6, center: new google.maps.LatLng(-33.8688, 151.2093) }, map = new google.maps.Map(document.getElementById('map-canvas'), myOptions), marker = new google.maps.Marker({ map: map, }), infowindow = new google.maps.InfoWindow; map.addListener('rightclick', function(e) { map.setCenter(e.latLng); marker.setPosition(e.latLng); infowindow.setContent("Latitude: " + e.latLng.lat() + "<br>" + "Longitude: " + e.latLng.lng()); infowindow.open(map, marker); }); } 
 html, body { height: 100%; } #map-canvas { height: 100%; width: 100%; } 
 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAIPPUQ0PSWMjTsgvIWRRcJv3LGfRzGmnA&callback=initMap" async defer></script> <div id="map-canvas"></div>