-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecondlifeAllInOne.html
157 lines (140 loc) · 4.68 KB
/
SecondlifeAllInOne.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<title>Second Life</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
html, body, #map {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCFRKZ7lst3xlFDNjScBknBeEndp4XLj_Q"></script>
<script>
var findNearestTraumaCenter = function() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var size = 0;
var currentPosition;
// An array to store results calculated using Google routing API.
var routeResults = [];
// An array of trauma centers.
var listOfTraumaCenters = [
{'title': 'Hospital Sri Krishna Sevashrama, Jaya Nagar', 'latLng': new google.maps.LatLng(12.917584, 77.585133)},
{'title': 'Maiya Multispeciality Hospital, Jaya Nagar', 'latLng': new google.maps.LatLng(12.942653, 77.585427)},
{'title': 'Manasa Neuropsychiatric Hospital, Jaya Nagar', 'latLng': new google.maps.LatLng(12.940202, 77.585398)},
{'title': 'Sino Vedic Cancer Clinic, Jaya Nagar', 'latLng': new google.maps.LatLng(12.928227, 77.581766)}
];
// initialize on page load to find nearest trauma centers from current location
function initialize(currentLat, currentLng) {
currentPosition = new google.maps.LatLng(currentLat, currentLng);
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
zoom: 12,
center: currentPosition
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: currentPosition,
map: map,
label: {
color: 'black',
fontWeight: 'bold',
text: 'Currrent location.',
},
icon: {
labelOrigin: new google.maps.Point(11, 50),
//dummy url kept on purpose to create space
url: 'dummy.png',
size: new google.maps.Size(22, 40),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(11, 40),
}
});
var i = listOfTraumaCenters.length;
while (i--) {
listOfTraumaCenters[i].marker = new google.maps.Marker({
position: listOfTraumaCenters[i].latLng,
map: map,
label: {
color: 'black',
fontWeight: 'bold',
text: listOfTraumaCenters[i].title,
},
icon: {
labelOrigin: new google.maps.Point(11, 50),
//dummy url kept on purpose to create space
url: 'dummy.png',
size: new google.maps.Size(22, 40),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(11, 40),
}
});
}
findNearestPlaceTraumaCenter();
}
//Loop through all trauma centers to calculate route between the current location and trauma center
// Future : Filter by city / zip code to minimize th number of route calculation
function findNearestPlaceTraumaCenter() {
var i = listOfTraumaCenters.length;
size = listOfTraumaCenters.length;
routeResults = [];
while (i--) {
calculateRoute(listOfTraumaCenters[i].latLng, storeResult);
}
}
// A function to calculate the fastest driving route between two locations.
//Future: Calculate all possible travel mode
function calculateRoute(end, callback) {
var request = {
origin: currentPosition,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
callback(response);
} else {
size--;
}
});
}
// Stores a routing result calculated using google API
function storeResult(data) {
routeResults.push(data);
if (routeResults.length === size) {
findShortest();
}
}
// Loops through all the routes to thr route with shortest distance. Sets that route for the user to see.
// Future: Find best route not just by distance but also by time, driving mode and other parameters
function findShortest() {
var i = routeResults.length;
var shortestIndex = 0;
var shortestLength = routeResults[0].routes[0].legs[0].distance.value;
while (i--) {
if (routeResults[i].routes[0].legs[0].distance.value < shortestLength) {
shortestIndex = i;
shortestLength = routeResults[i].routes[0].legs[0].distance.value;
}
}
directionsDisplay.setDirections(routeResults[shortestIndex]);
}
// Expose the initialize function publicly as "init".
return {
init: initialize
};
}();
// Upon page load, find the nearest trauma center. Current location is hardcoded.
// Find current location based on browser API
google.maps.event.addDomListener(window, 'load', findNearestTraumaCenter.init(12.925381, 77.585848));
</script>
</body>
</html>