-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocation2.html
92 lines (87 loc) · 3.07 KB
/
geolocation2.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Create a polyline using Geolocation and Google Maps API</title>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
// If the browser supports the Geolocation API
if (typeof navigator.geolocation == "undefined") {
$("#error").text("Your browser doesn't support the Geolocation API");
return;
}
// Save the positions' history
var path = [];
navigator.geolocation.watchPosition(function(position) {
// Save the current position
path.push(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
// Create the map
var myOptions = {
zoom : 16,
center : path[0],
mapTypeId : google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
/*
Uncomment this block if you want to set a path
// Create the polyline's points
for(var i = 0; i < 5; i++) {
// Create a random point using the user current position and a random generated number.
// The number will be once positive and once negative using based on the parity of i
// and to reduce the range the number is divided by 10
path.push(
new google.maps.LatLng(
position.coords.latitude + (Math.random() / 10 * ((i % 2) ? 1 : -1)),
position.coords.longitude + (Math.random() / 10 * ((i % 2) ? 1 : -1))
)
);
}
*/
// Create the array that will be used to fit the view to the points range and
// place the markers to the polyline's points
var latLngBounds = new google.maps.LatLngBounds();
for(var i = 0; i < path.length; i++) {
latLngBounds.extend(path[i]);
// Place the marker
new google.maps.Marker({
map: map,
position: path[i],
title: "Point " + (i + 1)
});
}
// Creates the polyline object
var polyline = new google.maps.Polyline({
map: map,
path: path,
strokeColor: '#0000FF',
strokeOpacity: 0.7,
strokeWeight: 1
});
// Fit the bounds of the generated points
map.fitBounds(latLngBounds);
},
function(positionError){
$("#error").append("Error: " + positionError.message + "<br />");
},
{
enableHighAccuracy: true,
timeout: 10 * 1000 // 10 seconds
});
});
</script>
<style type="text/css">
#map {
width: 500px;
height: 400px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>Create a polyline</h1>
<div id="map"></div>
<p id="error"></p>
</body>
</html>