Google Maps API 3: Get Right Click Coordinates

I have 2 text fields for lat and lon when the user enters a new entry in db.

I have a live Google Maps view that works fine, now what I want to do is add a right-click on the map, which fills the lat / lon text fields with the button pressed.

Is it possible?

I know how to add an event listener, and looked through the API docs, but did not see anything that does this. I know that you can do this on a Google map on your website.

+55
google-maps google-maps-api-3
Oct 26 '11 at 16:25
source share
2 answers
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); }); 
+151
Oct 26 2018-11-18T00:
source share

You can create an InfoWindow object ( cool documentation here ) and attach a rightclick event handler to it, which will fill it with the latitude and longitude of the clicked location on the map.

 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> 
+3
Jan 30 '17 at 11:09 on
source share



All Articles