sl@0
|
1 |
|
sl@0
|
2 |
|
sl@0
|
3 |
<!DOCTYPE html>
|
sl@0
|
4 |
<html>
|
sl@0
|
5 |
<head>
|
sl@0
|
6 |
<title>Geolocation</title>
|
sl@0
|
7 |
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
|
sl@0
|
8 |
<meta charset="utf-8">
|
sl@0
|
9 |
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
|
sl@0
|
10 |
<!--
|
sl@0
|
11 |
Include the maps javascript with sensor=true because this code is using a
|
sl@0
|
12 |
sensor (a GPS locator) to determine the user's location.
|
sl@0
|
13 |
See: https://developers.google.com/apis/maps/documentation/javascript/basics#SpecifyingSensor
|
sl@0
|
14 |
-->
|
sl@0
|
15 |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
|
sl@0
|
16 |
|
sl@0
|
17 |
<script>
|
sl@0
|
18 |
var map;
|
sl@0
|
19 |
|
sl@0
|
20 |
function initialize() {
|
sl@0
|
21 |
var mapOptions = {
|
sl@0
|
22 |
zoom: 6,
|
sl@0
|
23 |
mapTypeId: google.maps.MapTypeId.ROADMAP
|
sl@0
|
24 |
};
|
sl@0
|
25 |
map = new google.maps.Map(document.getElementById('map-canvas'),
|
sl@0
|
26 |
mapOptions);
|
sl@0
|
27 |
|
sl@0
|
28 |
// Try HTML5 geolocation
|
sl@0
|
29 |
if(navigator.geolocation) {
|
sl@0
|
30 |
navigator.geolocation.getCurrentPosition(function(position) {
|
sl@0
|
31 |
var pos = new google.maps.LatLng(position.coords.latitude,
|
sl@0
|
32 |
position.coords.longitude);
|
sl@0
|
33 |
|
sl@0
|
34 |
var infowindow = new google.maps.InfoWindow({
|
sl@0
|
35 |
map: map,
|
sl@0
|
36 |
position: pos,
|
sl@0
|
37 |
content: 'Location found using HTML5.'
|
sl@0
|
38 |
});
|
sl@0
|
39 |
|
sl@0
|
40 |
map.setCenter(pos);
|
sl@0
|
41 |
}, function() {
|
sl@0
|
42 |
handleNoGeolocation(true);
|
sl@0
|
43 |
});
|
sl@0
|
44 |
} else {
|
sl@0
|
45 |
// Browser doesn't support Geolocation
|
sl@0
|
46 |
handleNoGeolocation(false);
|
sl@0
|
47 |
}
|
sl@0
|
48 |
}
|
sl@0
|
49 |
|
sl@0
|
50 |
function handleNoGeolocation(errorFlag) {
|
sl@0
|
51 |
if (errorFlag) {
|
sl@0
|
52 |
var content = 'Error: The Geolocation service failed.';
|
sl@0
|
53 |
} else {
|
sl@0
|
54 |
var content = 'Error: Your browser doesn\'t support geolocation.';
|
sl@0
|
55 |
}
|
sl@0
|
56 |
|
sl@0
|
57 |
var options = {
|
sl@0
|
58 |
map: map,
|
sl@0
|
59 |
position: new google.maps.LatLng(60, 105),
|
sl@0
|
60 |
content: content
|
sl@0
|
61 |
};
|
sl@0
|
62 |
|
sl@0
|
63 |
var infowindow = new google.maps.InfoWindow(options);
|
sl@0
|
64 |
map.setCenter(options.position);
|
sl@0
|
65 |
}
|
sl@0
|
66 |
|
sl@0
|
67 |
google.maps.event.addDomListener(window, 'load', initialize);
|
sl@0
|
68 |
|
sl@0
|
69 |
</script>
|
sl@0
|
70 |
</head>
|
sl@0
|
71 |
<body>
|
sl@0
|
72 |
<div id="map-canvas"></div>
|
sl@0
|
73 |
</body>
|
sl@0
|
74 |
</html>
|
sl@0
|
75 |
|