This repository has been archived by the owner on Dec 5, 2017. It is now read-only.
/
index.html
47 lines (45 loc) · 1.85 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<html>
<head><title>Geolocation</title>
<meta name="viewport" content="width=device-width maximum-scale=1 initial-scale=1 user-scalable=no">
<link rel="stylesheet" type="text/css" href="../../style.css"/>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"></script>
</head>
<body>
<h2>Geolocation</h2>
<p id="progress">Please wait...</p>
<p>Latitude: <span id="latitude">unknown</span><br>
Longitude: <span id="longitude">unknown</span></p>
<p>Approximate address: <span id="geocode">unavailable</span></p>
<script>
var el = document.getElementById('progress'),
latitude = document.getElementById('latitude'),
longitude = document.getElementById('longitude'),
geocode = document.getElementById('geocode');
if (navigator.geolocation) {
el.textContent = 'Location is obtained.';
window.onload = function() {
var geocoder = new google.maps.Geocoder();
navigator.geolocation.watchPosition(function (position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
latitude.textContent = position.coords.latitude.toFixed(2);
longitude.textContent = position.coords.longitude.toFixed(2);
geocoder.geocode({'latLng': pos}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results.length && results[0].formatted_address) {
geocode.textContent = results[0].formatted_address;
}
} else {
geocode.textContent = 'unavailable (' + status + ')';
}
});
}, function (error) {
el.textContent = 'Error: ' + error;
});
};
} else {
el.textContent = 'Sorry! Your browser does not support geolocation.';
}
</script>
</body>
</html>