<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Geocoding service</title>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
geocoder = new google.maps.Geocoder();
address = "toronto, canada";
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var lat = results[0].geometry.location.lat();
var lng = results[0].geometry.location.lng();
} else {
var lat = 123;
var lng = 456;
}
document.getElementById('my_place1').value = lat + " - " + lng;
});
document.getElementById('my_place').value = lat + " - " + lng;
}
</script>
</head>
<body onLoad="initialize()">
<input name="" id="my_place" style="width:300px; margin-left:20px;" type="text">
<br>
<input name="" id="my_place1" style="width:300px; margin-left:20px;" type="text">
</body>
</html>
In the above code for my_place1 value is working but the same value not working with my_place. Can anybody help to solve this issue? I want to work something outside geocoder.geocode with lat and lng values.
latandlngare out of scope.