-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindexDistance.html
120 lines (100 loc) · 4 KB
/
indexDistance.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function loadMap(idMapCanvas, latitude, longitude, accuracy) {
var latlng = new google.maps.LatLng(latitude, longitude);
var optionsMap = {
zoom : 15,
center : latlng,
mapTypeId : google.maps.MapTypeId.HYBRID
};
var map = new google.maps.Map(document.getElementById(idMapCanvas), optionsMap);
var optionsCircle = {
center : latlng,
map : map,
radius : parseInt(accuracy),
clickable : false,
fillColor : "#00AAFF",
fillOpacity : 0.3,
strokeColor : "#057CB8",
strokeOpacity : 0.8,
strokeWeight : 1
};
var circle = new google.maps.Circle(optionsCircle);
var infowindow = new google.maps.InfoWindow();
infowindow.setContent(latitude + ", " + longitude);
var optionsMarker = {
position : latlng,
flat : true,
title : latlng.toString(),
map : map
};
var marker = new google.maps.Marker(optionsMarker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
}
var distances = new Array();
function calculateDistance(origin, destination, totalDestinations) {
var directionsRequest = {origin: origin, destination: destination.coords, unitSystem: google.maps.DirectionsUnitSystem.METRIC,
travelMode: google.maps.DirectionsTravelMode.WALKING};
var directionsService = new google.maps.DirectionsService();
directionsService.route(directionsRequest, onSuccessDirectionsService);
function onSuccessDirectionsService(directionsResult, directionsStatus) {
if (directionsStatus == google.maps.DirectionsStatus.OK) {
var result = destination.name + ": " + directionsResult.routes[0].legs[0].distance.text;
distances.push({result: result, value: directionsResult.routes[0].legs[0].distance.value});
if (distances.length == totalDestinations)
shortestDistance(distances);
} else
alert(directionsStatus);
}
}
function shortestDistance(distances) {
alert(distances.sort(sortDistances)[0].result);
}
function sortDistances(a, b) {
return a.value - b.value;
}
function nearestPoint(latitude, longitude) {
var origin = new google.maps.LatLng(latitude, longitude);
var destinations = [{name: "Avantic", coords: new google.maps.LatLng("28.46248389570779", "-16.293938564418")},
{name: "El Corte Inglés", coords: new google.maps.LatLng("28.460061", "-16.253554")},
{name: "Las Teresitas", coords: new google.maps.LatLng("28.507202", "-16.189342")}
];
for (i = 0; i < destinations.length; i++)
calculateDistance(origin, destinations[i], destinations.length);
}
function initGeolocation() {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
function onSuccess(position){
document.getElementById('loading').style.display = "none";
loadMap("map_canvas", position.coords.latitude, position.coords.longitude, position.coords.accuracy);
nearestPoint(position.coords.latitude, position.coords.longitude);
}
function onError(error)
{
document.getElementById('loading').style.display = "none";
switch(error.code)
{
case error.PERMISSION_DENIED: alert("No se ha permitido compartir los datos de geolocalización");
break;
case error.POSITION_UNAVAILABLE: alert("No se puede detectar la posición actual");
break;
case error.TIMEOUT: alert("Se ha superado el tiempo de espera");
break;
default: alert("Error desconocido");
break;
}
}
</script>
</head>
<body onload="initGeolocation()">
<span id="loading">Cargando...</span>
<div id="map_canvas" style="width: 500px; height: 500px;"></div>
</body>
</html>