-
Notifications
You must be signed in to change notification settings - Fork 6
/
mapComponent.html
566 lines (490 loc) · 20.1 KB
/
mapComponent.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
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
<!DOCTYPE html>
<html>
<head>
<title>App Inventor - Map Component</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
margin: 0px;
padding: 0px
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script>
<script>
/**
* This map script is an abstraction of a number of functions from the Google maps
* JavaScript API to be used within a customized WebView as an App Inventor Component.
*
* It contains two main objects: thisMap and mapMarkers.
* thisMap initializes the map and makes use of mapMarkers, which simply encapsulates all
* functions related to markers placed in the map. It is possible to create other utility
* objects with other functionality in the SDK such as drawing, layers, or services.
*/
/**
* This function returns an object with certain methods exposed as its API. The functionality
* of this object is related to management of markers in the map.
* @returns an Object with methods related to Marker management.
* @param mapComponent the map to associate the markers with.
*/
var mapMarkers = function(mapComponent) {
if (!mapComponent) throw new Error('No map available');
function AIMarker(marker){
if (marker instanceof google.maps.Marker){
this.marker = marker;
this.id = marker.getPosition().toString(); //Use position as unique id
}
else {
console.log('Calling Error handler on Android side - Invalid Marker');
androidObject.dispatchErrorToAndroid(androidObject.ERROR_INVALID_MARKER);
}
}
AIMarker.prototype.equals = function(otherMarker){
if (otherMarker == null) return false;
if (otherMarker instanceof AIMarker){
//Equality is based on position - two markers on the same position are the same marker
return this.marker.getPosition().toString() ===
otherMarker.marker.getPosition().toString();
}
return false;
}
var aiMarkers = {};
/**
* Add a marker, with additional information, to the map.
* @param location {google.maps.LatLng} object specifying the position in the map
* @param infoWindowContent content to be displayed in this marker infoWindow
* @param title a title for the marker (shown on hover in browsers)
*/
var addAIMarker = function(location, title, infoWindowContent){
var newAiMarker, marker;
if (location instanceof google.maps.LatLng){
newAiMarker = aiMarkers[location.toString()];
if (newAiMarker) { //If it exists, there's no need to create it
marker = newAiMarker.marker;
// We override values even if they are not different - it's easier.
marker.title = title;
if (infoWindowContent && marker.info){
marker.info.setContent(infoWindowContent);
}
}
else {
marker = new google.maps.Marker({
position: location,
title: title || '',
map: mapComponent
});
newAiMarker = new AIMarker(marker);
var theId = newAiMarker.id;
google.maps.event.addListener(marker, 'click', markerClicked(theId));
google.maps.event.addListener(marker, 'dblclick', markerClicked(theId, true));
aiMarkers[marker.getPosition().toString()] = newAiMarker;
if (infoWindowContent) {
createInfoWindow(theId, infoWindowContent);
}
}
}
else {
console.log('Calling Error handler on Android side');
androidObject.dispatchErrorToAndroid(androidObject.ERROR_ILLEGAL_COORDS_FORMAT);
}
return newAiMarker;
};
// Closure needed to associate each markerId with its click handler function
function markerClicked(markerId, doubleClick) {
return function(){
handleMarkerById(markerId, doubleClick);
}
}
function handleMarkerById(markerId, doubleClick) {
var markerJson = createJsonMarkerFromId(markerId);
if (doubleClick)
androidObject.sendDoubleMarkerToAndroid(markerJson);
else{
androidObject.sendMarkerToAndroid(markerJson);
}
}
function createJsonMarkerFromId(markerId){
var currentAiMarker = aiMarkers[markerId];
var markerObject = {
lat: currentAiMarker.marker.getPosition().lat(),
lng: currentAiMarker.marker.getPosition().lng(),
title: currentAiMarker.marker.title || '',
info: (currentAiMarker.marker.info && currentAiMarker.marker.info.content) ?
currentAiMarker.marker.info.content : ''
}
var markerJson = JSON.stringify(markerObject);
return markerJson;
}
var panToMarker = function(markerId) {
var markerToPanTo = aiMarkers[markerId].marker;
if (markerToPanTo)
mapComponent.panTo(markerToPanTo.getPosition());
};
/**
* Decodes the JSON input and generates and displays markers for each of the objects.
* Sample data:
* "[
* {"lat":48.856614,"lng":2.3522219000000177,"title":"","content":""},
* {"lat":48,"lng":3,"title":"near paris","content":"near Paris content"}
* ]"
* @param listOfMarkers a JSON representation of the markers to be displayed
*/
var addMarkersFromList = function(listOfMarkers) {
var allMarkers = [];
try{
allMarkers = JSON.parse(listOfMarkers);
}
catch(parseError) {
console.log('List of Markers is not valid JSON. Notifying Android side.');
androidObject.dispatchErrorToAndroid(androidObject.ERROR_PARSING_MARKERS_LIST)
}
function decodeMarker(markerObject){
var markerData = [];
var lat, lng;
if (markerObject.lat)
lat = markerObject.lat;
if (markerObject.lng)
lng = markerObject.lng;
if (lat && lng)
markerData[0] = locationFromLatLngCoords(lat, lng);
if (markerObject.title)
markerData[1] = markerObject.title;
if (markerObject.content)
markerData[2] = markerObject.content;
// Location has to be available(other fields are optional).
if (markerData[0] instanceof google.maps.LatLng)
return markerData;
else //TODO (jos) trigger user feedback or not?
return null;
}
allMarkers.forEach(function(markerObject) {
// Try to decode each marker and even if some fail, still add the others.
var markerData = decodeMarker(markerObject);
if (markerData)
addAIMarker(markerData[0], markerData[1], markerData[2]);
});
};
var locationFromLatLngCoords = function(lat, lng){
var errorParsing = false;
if (isNaN(lat) || isNaN(lng)) errorParsing = true;
if (lat < -90 || lat > 90) errorParsing = true;
if (lng < -180 || lng > 180) errorParsing = true;
if (errorParsing){
androidObject.dispatchErrorToAndroid(androidObject.ERROR_ILLEGAL_COORDS_FORMAT);
return null;
}
else
return new google.maps.LatLng(lat, lng);
};
/**
* Generates a LatLng map object from coordinates passed in as a string. Valid ranges are:
* Lat [-90, 90], and Lng [-180, 180].
* @param locationText a string in the format 'float, float'
* @returns {google.maps.LatLng} a LatLng object or null if the location is not in the
* right format.
*/
var locationFromTextCoords = function(locationText) {
var lat, lng;
var locationSplit = locationText.split(',');
if (locationSplit.length === 2){
lat = parseFloat(locationSplit[0]);
lng = parseFloat(locationSplit[1]);
return locationFromLatLngCoords(lat, lng);
}
else {
androidObject.dispatchErrorToAndroid(androidObject.ERROR_ILLEGAL_COORDS_FORMAT);
return null;
}
};
var addListenersForMarkers = function (add) {
if (add){
google.maps.event.addListener(mapComponent, 'click', function(event) {
var aiMarker = addAIMarker(event.latLng);
var markerJson = createJsonMarkerFromId(aiMarker.id);
androidObject.sendUserMarkerAddedToAndroid(markerJson);
});
}
else
google.maps.event.clearListeners(mapComponent,'click');
};
var showMarker = function(markerId, show) {
var markerToShow = aiMarkers[markerId].marker;
if (show) {
if (markerToShow){
markerToShow.setMap(mapComponent);
}
}
else {
if (markerToShow) {
markerToShow.setMap(null);
}
}
};
var showMarkers = function (show){
if (show) {
for (var key in aiMarkers){
aiMarkers[key].marker.setMap(mapComponent);
}
}
else {
for (var key in aiMarkers){
aiMarkers[key].marker.setMap(null);
}
}
};
var deleteMarkers = function (){
showMarkers(false);
aiMarkers = {};
};
var storeMarkers = function () {
var allMarkers = [];
for (var key in aiMarkers){
var marker = aiMarkers[key].marker;
var jsMarker = {};
jsMarker.lat = marker.getPosition().lat();
jsMarker.lng = marker.getPosition().lng();
jsMarker.title = marker.title || '';
if (marker.info && marker.info.content)
jsMarker.content = marker.info.content;
else
jsMarker.content = '';
allMarkers.push(jsMarker);
}
androidObject.sendListOfMarkersToAndroid(JSON.stringify(allMarkers));
return allMarkers;
};
//Geolocation service
var geolocate = function (address){
var geocoder = new google.maps.Geocoder();
geocoder.geocode({address: address}, geolocationResults)
};
function geolocationResults(results, status){
var notifyAndroid = false;
if (status === 'OK') {
var firstLocationFound = results[0].geometry.location;
if (firstLocationFound){
console.log(firstLocationFound);
var marker = addAIMarker(firstLocationFound);
var markerJson = createJsonMarkerFromId(marker.id);
androidObject.sendGeolocationMarkerAddedToAndroid(markerJson,
results[0].formatted_address);
}
else {
console.log('No location found!');
notifyAndroid = true;
}
}
else if (status === "ZERO_RESULTS"){
console.log('No results found for that particular address.');
notifyAndroid = true;
}
else {
console.log('No results found. Status of Geolocation call: ' + status);
notifyAndroid = true;
}
if (notifyAndroid) {
console.log('Notifying the Android side: No Results from Geolocation.')
androidObject.dispatchErrorToAndroid(androidObject.ERROR_NO_GEOLOCATION_RESULTS);
}
};
// InfoWindow functions
var createInfoWindow = function(markerId, content) {
var infoWindow = new google.maps.InfoWindow({
content: content
});
var marker = aiMarkers[markerId].marker;
if (marker)
marker.info = infoWindow;
};
var openInfoWindow = function(markerId){
var marker = aiMarkers[markerId].marker;
if (marker && marker.info)
marker.info.open(mapComponent, marker);
};
var closeInfoWindow = function(markerId){
var marker = aiMarkers[markerId].marker;
if (marker && marker.info)
marker.info.close();
};
var setMarkerTitle = function(markerId, title) {
var marker = aiMarkers[markerId].marker;
if (marker)
marker.setTitle(title);
};
//API for the mapMarkers object
return {
addMarkersFromList: addMarkersFromList,
addListenersForMarkers: addListenersForMarkers,
showMarker: showMarker,
showMarkers: showMarkers,
deleteMarkers: deleteMarkers,
storeMarkers: storeMarkers,
geolocate: geolocate,
panToMarker: panToMarker,
createInfoWindow: createInfoWindow,
openInfoWindow: openInfoWindow,
closeInfoWindow: closeInfoWindow,
locationFromTextCoords: locationFromTextCoords,
locationFromLatLngCoords: locationFromLatLngCoords,
setMarkerTitle: setMarkerTitle,
addAIMarker: addAIMarker,
aiMarkers: aiMarkers
};
};
/**
* Main function for this script. Initializes the map and all the related functionality.
*
*/
var thisMap = function(centerLat, centerLng, showCenter, initialZoom){
var map;
var centerMarker;
var markerFunctions;
var zoom = initialZoom || 6;
var showingCenter = showCenter || true;
function initialize() {
var mapOptions = {
zoom: zoom,
center: new google.maps.LatLng(centerLat, centerLng),
disableDoubleClickZoom: true
};
mapComponent = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Listening for the first 'idle' event to tell Android that the map is ready
google.maps.event.addListenerOnce(mapComponent, 'idle', function(){
console.log('Triggering mapIsReady on Android side.');
androidObject.mapIsReadyToAndroid();
});
centerMarker = createCenter();
showCenter(showingCenter);
//Initialize marker functions object
markerFunctions = mapMarkers(mapComponent);
}
var createCenter = function(){
if (centerMarker) centerMarker.setMap(null); // Delete any existing center first.
var newCenter;
if (mapComponent && mapComponent.getCenter()){
newCenter = new google.maps.Marker({
position: mapComponent.getCenter(),
map: mapComponent,
title: 'Map Center',
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 6
}
});
}
return newCenter;
};
// Special case of showing a marker only for the center. Might be possible to abstract in
// markerFunctions, but leaving it here for now (jos).
var showCenter = function(show){
if (show){
if (centerMarker)
centerMarker.setMap(mapComponent);
else
centerMarker = createCenter();
}
else
centerMarker.setMap(null);
showingCenter = show;
};
var setCenter = function(location){
if (location instanceof google.maps.LatLng){
mapComponent.setCenter(location);
}
else mapComponent.setCenter(markerFunctions.locationFromTextCoords(location));
centerMarker = createCenter();
showCenter(showingCenter);
};
var getMap = function() { return mapComponent; };
var getMarkerFunctions = function() { return markerFunctions; };
var setZoom = function(zoom) {
if (zoom >= 0 && zoom <= 19){
mapComponent.setZoom(zoom);
}
else // Exception handling is also done on Android side
console.log('Zoom value ' + zoom + ' is not in the valid range 0-19');
};
var getZoom = function() { return mapComponent.zoom; };
//API for the thisMap object: main entry object for functionality
return {
initialize: initialize,
showCenter: showCenter,
setCenter: setCenter,
getMap: getMap, // For Debugging (not used from the component).
getMarkerFunctions: getMarkerFunctions,
setZoom: setZoom,
getZoom: getZoom
}
//TODO (jos) Magic numbers: the center of the map will come from Android
}(43.473847, -8.169154, true, 6); //Auto initialize the thisMap object
/**
* An object to hold functions that communicate directly to Android through the JS interface.
* @type {{ERROR_ILLEGAL_COORDS_FORMAT: number, ERROR_PARSING_MARKERS_LIST: number,
* ERROR_NO_GEOLOCATION_RESULTS: number, dispatchErrorToAndroid: dispatchErrorToAndroid,
* sendMarkerToAndroid: sendMarkerToAndroid,
* sendDoubleMarkerToAndroid: sendDoubleMarkerToAndroid,
* sendListOfMarkersToAndroid: sendListOfMarkersToAndroid,
* mapIsReadyToAndroid: mapIsReadyToAndroid,
* sendUserMarkerAddedToAndroid: sendUserMarkerAddedToAndroid,
* sendGeolocationMarkerAddedToAndroid: sendGeolocationMarkerAddedToAndroid}}
*/
var androidObject = {
// CONSTANTS FOR ERRORS, As defined on the Android side.
ERROR_ILLEGAL_COORDS_FORMAT: 2702,
ERROR_PARSING_MARKERS_LIST: 2703,
ERROR_INVALID_MARKER: 2704,
ERROR_NO_GEOLOCATION_RESULTS: 2706,
/**
* Function to dispatch errors to Android through the AppInventorMap interface. If this
* file is loaded on a browser, AppInventorMap will be undefined and we skip the
* dispatching.
* TODO (jos) think about Error handling in JS if I even want to use this file standalone.
* @param errorNumber number for the message to display as user feedback on the Android
* side. The messages are defined in ErrorMessages.java
*/
dispatchErrorToAndroid: function(errorNumber) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.dispatchError(errorNumber);
},
/**
* Function to call to the Android side after a user has clicked on a marker
*/
sendMarkerToAndroid: function(jsonMarker) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.handleMarker(jsonMarker);
},
sendDoubleMarkerToAndroid: function(jsonMarker) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.handleDoubleMarker(jsonMarker);
},
/**
* Function to export all markers to the Android side for storage
*/
sendListOfMarkersToAndroid: function(markers) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.storeMarkers(markers);
},
/**
* Notify Component that the Map is ready.
*/
mapIsReadyToAndroid: function() {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.mapIsReady();
},
sendUserMarkerAddedToAndroid: function(markerJson) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.userMarkerAdded(markerJson);
},
sendGeolocationMarkerAddedToAndroid: function(markerJson, formattedAddress) {
if (typeof AppInventorMap !== 'undefined')
AppInventorMap.geolocationMarkerAdded(markerJson, formattedAddress);
}
};
google.maps.event.addDomListener(window, 'load', thisMap.initialize);
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>