function initialize() {
var mapOptions = {
zoom: 14,
center: new google.maps.LatLng(38.8862447, -77.02158380000003),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
geocoder = new google.maps.Geocoder;
var fx = google.maps.InfoWindow.prototype.setPosition;
google.maps.InfoWindow.prototype.setPosition = function() {
if (this.logAsInternal) {
google.maps.event.addListenerOnce(this, 'map_changed', function() {
var map = this.getMap();
if (map) {
google.maps.event.trigger(map, 'click', {
latLng: this.getPosition()
});
}
});
}
fx.apply(this, arguments);
};
google.maps.event.addListener(map, 'click', function(e) {
geocoder.geocode({
'location': e.latLng
}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert('place id: ' + results[0].place_id);
} else {
console.log('No results found');
}
} else {
console.log('Geocoder failed due to: ' + status);
}
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
margin: 0;
height: 100%;
}
<div id="map_canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?callback=initialize" async defer></script>