-
Notifications
You must be signed in to change notification settings - Fork 2
/
ructaxi.js
196 lines (155 loc) · 5.49 KB
/
ructaxi.js
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var map, iconA, iconB, directionDisplay, directionsService = new google.maps.DirectionsService();
google.maps.Map.prototype.markers = new Array();
google.maps.Map.prototype.addMarker = function (marker) {
this.markers[this.markers.length] = marker;
if (marker.title == "A") {
(setMapCenter()(marker.getPosition()));
}
if (this.markers.length > 1) {
(redraw()());
}
};
google.maps.Map.prototype.getMarkers = function () {
return this.markers;
};
google.maps.Map.prototype.clearMarkers = function () {
for (var i = 0; i < this.markers.length; i++) {
this.markers[i].setMap(null);
}
this.markers = new Array();
};
var routings;
var talt = ni = 0;
var departLat, departLng, departCord, arriveLat, arriveLng, arriveCord;
function initmap() { //creates the map
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: true
});
var DKLatLng = createLatLng(55.6374355, 12.0897751); //Roskilde's coordinations
var options = {
zoom: 15,
center: DKLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
map = new google.maps.Map(document.getElementById("map"), options);
//directionsDisplay.setMap(map);
}
function initialize() {
iconB = new google.maps.MarkerImage("http://maps.gstatic.com/intl/en_ALL/mapfiles/icon_greenB.png", null, null, new google.maps.Point(0, 38));
iconA = new google.maps.MarkerImage("http://maps.gstatic.com/intl/en_ALL/mapfiles/icon_greenA.png", null, null, new google.maps.Point(0, 38));
google.maps.event.addListener(map, "click", function (event) {
createMarker(event.latLng);
});
}
function createMarker(pos) {
if (map.markers.length < 1) { //Create first marker "A"
var markerTitle = "A";
var markerIcon = iconA;
} else {
if (map.markers.length == 1) { //Create second marker "B"
var markerTitle = "B";
var markerIcon = iconB;
} else {
return
}
}
var marker = new google.maps.Marker({
position: pos,
map: map,
icon: markerIcon,
title: markerTitle,
draggable: true
});
google.maps.event.addListener(marker, "dragend", dragendEvent(marker));
map.addMarker(marker);
}
function dragendEvent(obj) {
return function() {
(redraw()());
}
}
function redraw() {
return function () {
//findRoute(this.getMarkers()[0].getPosition(), this.getMarkers()[1].getPosition()); //doesn't work
findRoute(map.markers[0].getPosition(), map.markers[1].getPosition());
}
}
function findRoute(a, b) { //Draws the route between 2 coordinations: a and b
directionsDisplay.setMap(map);
var request = {
origin: a,
destination: b,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
optimizeWaypoints: true,
provideRouteAlternatives: true
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
routings = response.routes;
talt = response.routes.length;
directionsDisplay.setDirections(response);
if (talt > 1) {
$("#alternative").attr("disabled", false);
} else {
$("#alternative").attr("disabled", "disabled");
}
setTimeout("dirOnHead(0)", 200);
}
});
}
function dirOnHead(routeIndex) {
$("#distance").html("Distance: <b>" + routings[routeIndex].legs[0].distance.text + "</b>");
var dist = routings[routeIndex].legs[0].distance.value;
var start = 40.00; //Starting fare for taxi
var p100m = 1.4; //Price for each 100m in Danish Krone
$(".titel").text("Alternative Routes: " + talt);
//Receive departure address
departLat = map.markers[0].getPosition().lat();
departLng = map.markers[0].getPosition().lng();
departCord = departLat + ',' + departLng;
$.ajax({
url: "http://geo.oiorest.dk/adresser/" + departCord + ".json?callback=?",
dataType: "json",
success: findDepartAddr
});
//Receive arrival address
arriveLat = map.markers[1].getPosition().lat();
arriveLng = map.markers[1].getPosition().lng();
arriveCord = arriveLat + ',' + arriveLng;
$.ajax({
url: "http://geo.oiorest.dk/adresser/" + arriveCord + ".json?callback=?",
dataType: "json",
success: findArriveAddr
});
//Calculate and show the price
$("#price").html("Price: <b>" + (start + (dist / 100 * p100m)).toFixed(2) + " kr</b>");
}
function findDepartAddr(adresse) { //designs the departure address and publish it on the top of page
var tekst = "From: " + adresse.vejnavn.navn + ' ' + adresse.husnr + ', ' + adresse.postnummer.nr + ' ' + adresse.postnummer.navn;
$("#departAddress").text(tekst);
}
function findArriveAddr(adresse) { //designs the arrival address and publish it on the top of page
var tekst = "To: " + adresse.vejnavn.navn + ' ' + adresse.husnr + ', ' + adresse.postnummer.nr + ' ' + adresse.postnummer.navn;
$("#arriveAddress").text(tekst);
}
function createLatLng(la, lo) {
return new google.maps.LatLng(la, lo);
}
function setMapCenter() {
return function (pos) {
map.setCenter(pos);
}
}
function alterit() {
ni = (ni === talt - 1) ? 0 : directionsDisplay.getRouteIndex() + 1;
directionsDisplay.setRouteIndex(ni);
dirOnHead(ni);
}
function starter() {
initmap();
initialize();
$("button#alternative").click(function () {
alterit();
});
}