-
Notifications
You must be signed in to change notification settings - Fork 0
/
carto.js
executable file
·404 lines (346 loc) · 16.4 KB
/
carto.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
(function() {
var geocodeCache = [];
function Cartograph(options) {
this.map = false,
this.actualMap = false,
this.el = false,
this.provider = false,
this.type = false,
this.sensor = false,
this.geocoder = false,
this.center = false,
this.debug = false,
this.geocodeCount = 0,
//this.geocodeCache = [],
this.markers = [],
this.polylines = [],
this.directions = [],
this.mapTypes = [],
this.unitMetrics = [],
this.travelModes = [],
this.animations = [],
this.realignedPolylines = [];
this.init(options);
}
Cartograph.prototype.init = function(options) {
// Capture our map points
/*
Unsure of tackling these just yet
this.directions = options.directions || false;/**/
// Detect map type
this.provider = options.provider || false;
this.type = options.type || false;
this.el = options.element || false;
this.center = options.center || false;
this.debug = options.debug || false;
this.zoom = options.zoom || 8;
this.markers = options.markers || [];
this.polylines = options.polylines || [];
this.directions = options.directions || [];
switch(this.provider) {
case 'gmap':
/*
NEED A BETTER WAY TO LOAD THE SCRIPT AND CHECK WHAT HAPPENED
var script = document.createElement("script");
script.src = "http://maps.googleapis.com/maps/api/js?sensor=false&callback=Carto.setupMap";
document.body.appendChild(script);
/**/
this.setupMap();
var obj = this;
break;
case 'ovi':
case 'bing':
alert('not supported');
break;
default:
this.detectMap();
this.getMapObj();
this.setupMap();
}
}
Cartograph.prototype.setupMap = function() {
this.getMapObj();
// If a text string is given we need to geocode the string to get the lat/long
if(typeof this.center == 'string') {
var obj = this;
switch(this.provider) {
case 'gmap':
this.animations = google.maps.Animation;
this.mapTypes = google.maps.MapTypeId;
for(var mt in this.mapTypes) {
if(this.type.toLowerCase() === this.mapTypes[mt].toLowerCase()) {
this.type = this.mapTypes[mt];
}
}
var thisIndex = geocodeCache.length;
geocodeCache.push()
this.geocoder.geocode( {'address': this.center }, function(results, status) {
obj.center = results[0].geometry.location;
obj.actualMap = obj.draw();
if(obj.actualMap) {
if(obj.markers.length) obj.pushMarkers(obj.markers, obj.actualMap)
if(obj.polylines.length) obj.pushPolylines(obj.polylines, obj.actualMap)
if(obj.directions.length) obj.pushDirections(obj.directions, obj.actualMap)
return obj;
}
});/**/
break;
case 'bing':
case 'ovi':
alert('Support not implemented');
break;
}
}
}
Cartograph.prototype.pushDirections = function(directions, theMap) {
this.directionsService = new google.maps.DirectionsService();
this.directionsDisplay = new google.maps.DirectionsRenderer();
this.travelModes = google.maps.TravelMode;
this.unitMetrics = google.maps.UnitSystem;
// Set travel modes
for(var key in this.travelModes) {
if(key === directions.travel.toUpperCase()) {
directions.travel = this.travelModes[key];
}
}
// Set unit metrics
for(var key in this.unitMetrics) {
if(key === directions.units.toUpperCase()) {
directions.units = this.unitMetrics[key];
}
}
var directionsObj = {
origin: directions.from,
destination: directions.to,
waypoints: directions.waypoints || {},
provideRouteAlternatives: directions.alternatives || false,
travelMode: directions.travel,
unitSystem: directions.units
};
// Prime the directions request
directionsDisplay = this.directionsDisplay;
this.directionsService.route(directionsObj, function(result, status) {
if(status === google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
this.directionsDisplay.setMap(theMap);
}
Cartograph.prototype.pushPolylines = function(polylines, theMap) {
var polyLength = polylines.length;
for(var i = 0; i < polyLength; i++) {
var polyline = polylines[i];
if(typeof polyline == 'object') {
var polylineBuilder = [];
for(j = 0; j < polyline.waypoints.length; j++) {
var waypoint = polyline.waypoints[j];
var polylineDetails = {totalPointers: polyline.waypoints.length, originalWaypoints: polyline.waypoints, polyline: polyline, builder: polylineBuilder, currentMarker: j};
if(typeof waypoint == 'object') {
var location = new google.maps.LatLng(waypoint.lat, waypoint.long);
polylineDetails.builder.push(location);
if(polylineDetails.totalPointers === polylineDetails.builder.length) {
var flightPath = new google.maps.Polyline({
path: polylineDetails.builder,
strokeColor: polylineDetails.polyline.colour,
strokeOpacity: polylineDetails.polyline.opacity,
strokeWeight: polylineDetails.polyline.weight
});
flightPath.setMap(theMap);
}
} else {
this.geocodeAddr(polyline.waypoints[j], function(location, polylineDetails, obj) {
obj.realignedPolylines = [];
polylineDetails.builder.push({location: location, address: polylineDetails.originalWaypoints[polylineDetails.currentMarker]});
if(polylineDetails.totalPointers === polylineDetails.builder.length) {
// So all of our polylines are now resolved but there is a good chance they are in the wrong order if you are using a multiple of addresses and lat/lngs
for(var k = 0; k < polylineDetails.totalPointers; k++) {
for(var l = 0; l < polylineDetails.totalPointers; l++) {
//console.log(polylineDetails.originalWaypoints[k], polylineDetails.builder[l]);
if(polylineDetails.originalWaypoints[k] === polylineDetails.builder[l].address) {
// console.log(polylineDetails.builder[l]);
obj.realignedPolylines[k] = polylineDetails.builder[l].location;
//console.log();
if(k === l) { } else {
//console.log('CHEAT', polylineDetails.builder[l].address);
}
} else if(polylineDetails.builder[l].address == undefined) {
obj.realignedPolylines[k] = polylineDetails.builder[l];
}
}
//console.log(polylineDetails.originalWaypoints.indexOf())
}
var flightPath = new google.maps.Polyline({
path: obj.realignedPolylines,
strokeColor: polylineDetails.polyline.colour,
strokeOpacity: polylineDetails.polyline.opacity,
strokeWeight: polylineDetails.polyline.weight
});
flightPath.setMap(theMap);/**/
//console.log(polylines);
//console.log(polylineDetails);
return;
}
}, polylineDetails, this);
}
}
} else {
}
}
}
Cartograph.prototype.pushMarkers = function(markers, theMap) {
var markerLength = markers.length,
obj = this;
for(var i = 0; i < markerLength; i++) {
if(typeof markers[i] === 'object') { // we have a full object to interpret
if(markers[i].location) {
this.geocodeAddr(markers[i].location, function(location, markerObj, obj) {
var newMarker = new google.maps.Marker({
position: location,
map: theMap,
icon: markerObj.image || '',
title: markerObj.name
});
var infowindow = new google.maps.InfoWindow({
content: markerObj.iwindow.content || '<div id="content"><div id="siteNotice"></div><h1 id="firstHeading" class="firstHeading">' + markerObj.name + '</h1></div>'
});
google.maps.event.addListener(newMarker, 'click', function() { infowindow.open(theMap,newMarker); });
obj.markers.push(newMarker);
}, markers[i], this, false);
//var markerInfo = markers[i]; // TODO: Pass marker info/object through to geocode callback (somehow)
/*this.geocoder.geocode( {'address': markers[i].location }, function(results, status) {
obj.markers.push(new google.maps.Marker({
position: results[0].geometry.location,
map: theMap,
title: markerInfo.name
}));
});/**/
} else {
var newMarker = new google.maps.Marker({ position: new google.maps.LatLng(markers[i].lat, markers[i].long), map: theMap, title: markers[i].name, icon: markers[i].image || '' });
// Basic infowindow implementation to make sure info is being retained in the markerOffset
var infowindow = new google.maps.InfoWindow({
content: markers[i].iwindow.content
});
google.maps.event.addListener(newMarker, 'click', function() { infowindow.open(theMap,newMarker); });
this.markers.push(newMarker);
}
} else {
this.geocodeAddr(markers[i], function(location, marker, obj) {
var newMarker = new google.maps.Marker({
position: location,
map: theMap,
// There is never a marker here because they are only passing the location name
title:marker
});
var infowindow = new google.maps.InfoWindow({
content: '<div id="content"><div id="siteNotice"></div><h1 id="firstHeading" class="firstHeading">' + marker + '</h1></div>'
});
google.maps.event.addListener(newMarker, 'click', function() { infowindow.open(theMap,newMarker); });
obj.markers.push(newMarker);
}, markers[i], this);
}
}
}
Cartograph.prototype.geocodeAddr = function(address, cb, pushbackObj, obj, flag) {
var carto = this,
flagMatch = false,
currentIndex = 0;
// Check if item is already in cache.
console.log('geocode cache length', geocodeCache.length);
for(var i = 0; i < geocodeCache.length; i++) {
if(geocodeCache[i].address == address) {
flagMatch = geocodeCache[i];
console.log('from the cache!');
}
}
if(flagMatch) {
console.log(flagMatch);
// they are already cached!
setTimeout(function() {
console.log(flagMatch.geocoder.geometry.location)
cb(flagMatch.geocoder.geometry.location, pushbackObj, obj);
}, 1000);
return false;
} else {
currentIndex = geocodeCache.length;
geocodeCache.push({address: address, callback: cb,pbObj: pushbackObj,obj: obj});
}
console.log(geocodeCache);
if(flag) {
console.log('TIMEOUT');
obj.geocoderCount = 0;
}
if(obj.geocoderCount % 10 == 0) {
setTimeout("Cartograph.geocodeAddr(address, cb, pushbackObj, obj, true)", 5000);
} else {
this.geocoder.geocode({
'address': address
}, function (results, status) {
if(status === 'OK') {
console.log(obj.type, address);
geocodeCache[currentIndex].geocoder = results[0];
cb(results[0].geometry.location, pushbackObj, obj);
}
});
obj.geocoderCount++;
}
} // Getting our geocode face on
Cartograph.prototype.authenticate = function() {}
Cartograph.prototype.mapFunctions = function() {}
Cartograph.prototype.detectMap = function() {
// Legacy function in case they choose to include the maps themself
this.type = (window.google)?'gmap':(window.Microsoft)?'bing':(window.ovi)?'ovi':false;
//Return false if none of our maps are supported
if(this.type === false) { this.callError('unsupported'); return false; }
}
Cartograph.prototype.getMapObj = function() {
// Grasp the map type and associate
switch (this.provider) {
case 'gmap':
this.geocoder = new google.maps.Geocoder();
this.map = window.google.maps;
break;
case 'bing':
return window.Microsoft.Maps;
break;
case 'ovi':
return window.ovi.mapsapi;
break;
}
return false;
/*return window.google || // Sample shim from paul irish for requestAnimationFrame
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 1000 / 60);
};/**/
}
Cartograph.prototype.reverseGeocode = function() {}
// Put the map on the canvas
Cartograph.prototype.draw = function() {
// For future implementations of other maps being integrated.
switch(this.provider) {
case 'gmap':
var myOptions = {
zoom: this.zoom,
center: this.center,
mapTypeId: this.type
};
return new google.maps.Map(document.getElementById(this.el.replace('#', '')), myOptions);
break;
case 'bing':
case 'ovi':
alert('Support not yet implemented');
return false;
break;
}
}
Cartograph.prototype.callError = function(str) {
//if(this.debug) {
if (console) { console.log(str); } else { alert(str); }
//}
}
// Cartograph.init(options);
if(!window.Carto){window.Carto=Cartograph;}//We create a shortcut for our framework, we can call the methods by $$.method();
})();