Geocoding and coordinates
by zzzrefiddle
HTML
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<div id="panel">
<input id="address" type="textbox" value="1771 Francisco Acuña de Figueroa, Montevideo">
<input type="button" value="Geocode" onclick="codeAddress()">
</div>
<div id="map-canvas"></div>
CSS
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
/* width: 600px; */
margin-left: -300px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
#address {
width: 510px;
}
JavaScript
var geocoder;
var map;
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(26.4667, 87.2667);
var mapOptions = {
zoom: 15,
center: latlng
}
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
codeAddress();
}
function codeAddress() {
var address = document.getElementById('address').value;
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
var infowindow = new google.maps.InfoWindow({
content: "Latitude: " + results[0].geometry.location.lat().toFixed(8) + "</br>Longitude: " + results[0].geometry.location.lng().toFixed(8)
});
infowindow.open(map, marker);
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
document.getElementById("address").onkeypress = function (e) {
if (!e) e = window.event;
if (e.keyCode == '13') {
codeAddress();
return false;
}
}