<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Google Maps JavaScript API v3 Example: Reverse Geocoding</title>
<link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" rel="stylesheet">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script src="http://www.google.com/jsapi"></script>
<script>
var geocoder;
var map;
var infowindow = new google.maps.InfoWindow();
var marker;
geocoder = new google.maps.Geocoder();
function initialize() {
var latlng = new google.maps.LatLng(40.730885,-73.997383);
var mapOptions = {
zoom: 8,
center: latlng,
mapTypeId: 'roadmap'
}
// alert(mapOptions);
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}
function codeLatLng() {
// var input = document.getElementById('latlng').value;
if(google.loader.ClientLocation)
{
latitude = google.loader.ClientLocation.latitude;
longitude = google.loader.ClientLocation.longitude;
}
// var input = '13.083,80.3';
var input = latitude + ',' + longitude;
alert(input);
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
/* map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: map
}); */
//infowindow.setContent(results[1].formatted_address);
alert(results[1].formatted_address);
//infowindow.open(map, marker);
} else {
alert('No results found');
}
} else {
alert('Geocoder failed due to: ' + status);
}
});
}
</script>
</head>
<body onload="codeLatLng()">
<div>
<input id="latlng" type="textbox" value="40.714224,-73.961452">
</div>
<div>
<input type="button" value="Reverse Geocode" onclick="codeLatLng()">
</div>
<div id="map_canvas" style="height: 90%; top:60px; border: 1px solid black;"></div>
</body>
</html>
0 comments:
Post a Comment