-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolocation.html
70 lines (54 loc) · 1.81 KB
/
geolocation.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Geolocalización</title>
</head>
<body>
<button id="requestButton">Detectar posición</button>
<button id="stopButton">Parar detección</button>
<div id="data"></div>
<div id="map" style="width: 100%; height: 500px;"></div>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<script>
var operationID = null; // almacena el id del watch
// solicitamos mapa de google
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 18,
center: new google.maps.LatLng(0, 0)
});
$("#requestButton").on('click', function(){
// solicita la posiciíon actual de manera asíncrona
operationID = navigator.geolocation.watchPosition(
function(data){ // cuando ha ido bien
console.log("Geolocalización conseguida", data);
var html = "<ul>";
html += "<li><strong>Latitud: </strong>" + data.coords.latitude + "</li>";
html += "<li><strong>Longitud: </strong>" + data.coords.longitude + "</li>";
html += "<li><strong>Precición: </strong>" + data.coords.accuracy + "</li>";
html += "</ul>";
$("#data").html(html);
map.setCenter(new google.maps.LatLng(
data.coords.latitude,
data.coords.longitude
));
},
function(data){ // cuando ha ido mal
console.log("Geolocalización fallida", error);
},
{ // opciones de confiuración
enableHighAccuracy: true,
maximumAge : 0,
timeout : 27000
}
);
console.log("Operation ID", operationID);
});
$("#stopButton").on('click', function(){
navigator.geolocation.clearWatch(operationID);
console.log("Detenemos geolocaliación");
});
</script>
</body>
</html>