This repository was archived by the owner on May 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
187 lines (160 loc) · 5.29 KB
/
scripts.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
jQuery(document).ready(function($) {
var motoRoadMap = {
geocoder: null,
directionsDisplay: null,
directionsService: new google.maps.DirectionsService(),
map: {
map: null,
options: {
center: { lat: 46.343215, lng: 2.608344},
zoom: 7
},
route: null
},
points: [],
avoidHighways: false,
initialize: function() {
this.geocoder = new google.maps.Geocoder();
this.map.map = new google.maps.Map(document.getElementById('map-canvas'), this.map.options);
this.directionsDisplay = new google.maps.DirectionsRenderer();
this.directionsDisplay.setMap(this.map.map);
/*if(navigator.geolocation) {
motoRoadMap.findPosition();
}*/
return this;
},
findPosition: function() {
navigator.geolocation.getCurrentPosition(function(position) {
motoRoadMap.map.map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
});
return this;
},
geocodePosition: function(address, jqElement) {
this.geocoder.geocode( { 'address': address }, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
motoRoadMap.map.map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: motoRoadMap.map.map,
position: results[0].geometry.location
});
console.log(marker);
jqElement.find('input').val(results[0].formatted_address).attr('class', 'success');
motoRoadMap.points[motoRoadMap.points.length] = {address: results[0].formatted_address, marker: marker};
} else {
jqElement.find('input').attr('class', 'error');
console.log(status);
}
});
return this;
},
reverseGeocodePosition: function(position) {
var input = document.getElementById('latlng').value;
var latlngStr = input.split(',', 2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);
this.geocoder.geocode({'latLng': latlng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
motoRoadMap.map.map.setZoom(11);
marker = new google.maps.Marker({
position: latlng,
map: motoRoadMap.map.map
});
motoRoadMap.points[motoRoadMap.points.length] = {address: position, marker: marker};
} else {
// Position introuvable
}
} else {
// Position introuvable
}
});
return this;
},
generateListPoints: function() {
this.removeListPoints();
$('.points .point').each(function() {
if($(this).find('input').val() != '')
motoRoadMap.geocodePosition($(this).find('input').val(), $(this));
});
return this;
},
removeListPoints: function() {
for(var i=0; i<this.points.length; i++)
this.points[i].marker.setMap(null);
this.points = [];
return this;
},
calculateRoute: function() {
var start = motoRoadMap.points[0].marker.getPosition();
var end = motoRoadMap.points[this.points.length-1].marker.getPosition();
var waypts = [];
for(var i=1; i<this.points.length-1; i++) {
waypts.push({
location: this.points[i].marker.getPosition(),
stopover: true
});
}
var request = {
origin: start,
destination: end,
avoidHighways: this.avoidHighways,
optimizeWaypoints: true,
waypoints: waypts,
travelMode: google.maps.TravelMode.DRIVING
};
this.directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
motoRoadMap.directionsDisplay.setDirections(response);
console.log(response);
var legs = response.routes[0].legs;
var duration = 0;
for(var i=0; i<legs.length; i++)
duration += parseInt(legs[i].duration.value);
motoRoadMap.map.route = {
direction: response,
duration: duration
};
}
});
return this;
}
};
google.maps.event.addDomListener(window, 'load', motoRoadMap.initialize());
$('.points .point input').on('change', function() {
motoRoadMap.generateListPoints();
});
$('.add').on('click', function() {
var point = $('.point.begin').clone();
var nbCheckPoint = $('.point.checkpoint').length;
var remover = $('<button title="supprimer ce noeud" class="remove">x</button>');
remover.on('click', function() {
$(this).parent('div.point').remove();
motoRoadMap.generateListPoints();
});
point.removeClass('begin').addClass('checkpoint').attr('id', 'checkpoint_' + (nbCheckPoint+1)).find('input').val('').attr('class', '').attr('placeholder', 'par');
point.find('label').text('Par :').attr('for', 'checkpoint_' + (nbCheckPoint+1)).after(remover);
point.on('change', function() {
motoRoadMap.generateListPoints();
});
if(nbCheckPoint == 0)
$('.point.begin').after(point);
else
$('.point.checkpoint:last').after(point);
});
$('#calculate_route').click(function() {
motoRoadMap.calculateRoute();
});
$('#avoid_highways').click(function() {
motoRoadMap.avoidHighways = $(this).is(':checked');
});
// Animation et autres
$('.animated-checkbox').click(function() {
var aim = $(this).data('aim');
if($(this).is(':checked')) {
$(this).parents(aim).addClass('checked').find('span.icon').attr('class', 'icon icon-check-1');
} else {
$(this).parents(aim).removeClass('checked').find('span.icon').attr('class', 'icon icon-check-empty');
}
})
});