forked from Yermo/nativescript-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapbox.ios.js
executable file
·573 lines (504 loc) · 18.7 KB
/
mapbox.ios.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
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
567
568
569
570
571
572
573
var mapbox = require("./mapbox-common");
var fs = require("file-system");
var imgSrc = require("image-source");
(function() {
// need to kick this off otherwise offline stuff won't work without first showing a map
MGLOfflineStorage.sharedOfflineStorage();
})();
mapbox._getMapStyle = function(input) {
var version = 9;
if (input === mapbox.MapStyle.LIGHT) {
return MGLStyle.lightStyleURLWithVersion(version);
} else if (input === mapbox.MapStyle.DARK) {
return MGLStyle.darkStyleURLWithVersion(version);
} else if (input === mapbox.MapStyle.OUTDOORS) {
return MGLStyle.outdoorsStyleURLWithVersion(version);
} else if (input === mapbox.MapStyle.SATELLITE) {
return MGLStyle.satelliteStyleURLWithVersion(version);
} else if (input === mapbox.MapStyle.HYBRID || mapbox.MapStyle.SATELLITE_STREETS) {
return MGLStyle.satelliteStreetsStyleURLWithVersion(version);
} else {
// custom url
return NSURL.alloc().initWithString(input);
}
};
mapbox.show = function (arg) {
return new Promise(function (resolve, reject) {
try {
var settings = mapbox.merge(arg, mapbox.defaults);
// if no accessToken was set the app may crash
if (settings.accessToken === undefined) {
reject("Please set the 'accessToken' parameter");
return;
}
var view = UIApplication.sharedApplication().keyWindow.rootViewController.view,
frameRect = view.frame,
mapFrame = CGRectMake(
settings.margins.left,
settings.margins.top,
frameRect.size.width - settings.margins.left - settings.margins.right,
frameRect.size.height - settings.margins.top - settings.margins.bottom
),
styleURL = mapbox._getMapStyle(settings.style);
MGLAccountManager.setAccessToken(settings.accessToken);
mapbox.mapView = MGLMapView.alloc().initWithFrameStyleURL(mapFrame, styleURL);
// TODO not sure this works as planned.. perhaps better to listen for rotate events ([..didrotate..] and fix the frame
mapbox.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
if (settings.center) {
var centerCoordinate = CLLocationCoordinate2DMake(settings.center.lat, settings.center.lng);
mapbox.mapView.setCenterCoordinateZoomLevelAnimated(centerCoordinate, settings.zoomLevel, false);
} else {
mapbox.mapView.setZoomLevelAnimated(settings.zoomLevel, false);
}
mapbox.mapView.showsUserLocation = settings.showUserLocation;
mapbox.mapView.attributionButton.hidden = settings.hideAttribution;
mapbox.mapView.logoView.hidden = settings.hideLogo;
mapbox.mapView.compassView.hidden = settings.hideCompass;
mapbox.mapView.rotateEnabled = !settings.disableRotation;
mapbox.mapView.scrollEnabled = !settings.disableScroll;
mapbox.mapView.zoomEnabled = !settings.disableZoom;
mapbox.mapView.allowsTilting = !settings.disableTilt;
mapbox.mapView.delegate = mapbox._delegate = MGLMapViewDelegateImpl.new().initWithCallback(
function () {
resolve();
}
);
mapbox._markers = [];
mapbox._addMarkers(settings.markers);
// wrapping in a little timeout since the map area tends to flash black a bit initially
setTimeout(function() {
view.addSubview(mapbox.mapView);
}, 500);
} catch (ex) {
console.log("Error in mapbox.show: " + ex);
reject(ex);
}
});
};
mapbox.hide = function (arg) {
return new Promise(function (resolve, reject) {
try {
mapbox.mapView.removeFromSuperview();
resolve("Done");
} catch (ex) {
console.log("Error in mapbox.hide: " + ex);
reject(ex);
}
});
};
mapbox.addMarkers = function (markers) {
return new Promise(function (resolve, reject) {
try {
mapbox._addMarkers(markers);
resolve();
} catch (ex) {
console.log("Error in mapbox.addMarkers: " + ex);
reject(ex);
}
});
};
mapbox._addMarkers = function(markers) {
if (!markers) {
return;
}
for (var m in markers) {
var marker = markers[m];
mapbox._markers.push(marker);
var lat = marker.lat;
var lng = marker.lng;
var point = MGLPointAnnotation.alloc().init();
point.coordinate = CLLocationCoordinate2DMake(lat, lng);
point.title = marker.title;
point.subtitle = marker.subtitle;
mapbox.mapView.addAnnotation(point);
}
};
mapbox.setCenter = function (arg) {
return new Promise(function (resolve, reject) {
try {
var animated = arg.animated || true;
var lat = arg.lat;
var lng = arg.lng;
var coordinate = CLLocationCoordinate2DMake(lat, lng);
mapbox.mapView.setCenterCoordinateAnimated(coordinate, animated);
resolve();
} catch (ex) {
console.log("Error in mapbox.setCenter: " + ex);
reject(ex);
}
});
};
mapbox.getCenter = function () {
return new Promise(function (resolve, reject) {
try {
var coordinate = mapbox.mapView.centerCoordinate;
resolve({
lat: coordinate.latitude,
lng: coordinate.longitude
});
} catch (ex) {
console.log("Error in mapbox.getCenter: " + ex);
reject(ex);
}
});
};
mapbox.setZoomLevel = function (arg) {
return new Promise(function (resolve, reject) {
try {
var animated = arg.animated || true;
var level = arg.level;
if (level >=0 && level <= 20) {
mapbox.mapView.setZoomLevelAnimated(level, animated);
resolve();
} else {
reject("invalid zoomlevel, use any double value from 0 to 20 (like 8.3)");
}
} catch (ex) {
console.log("Error in mapbox.setZoomLevel: " + ex);
reject(ex);
}
});
};
mapbox.getZoomLevel = function () {
return new Promise(function (resolve, reject) {
try {
var level = mapbox.mapView.zoomLevel;
resolve(level);
} catch (ex) {
console.log("Error in mapbox.getZoomLevel: " + ex);
reject(ex);
}
});
};
mapbox.setTilt = function (arg) {
return new Promise(function (resolve, reject) {
try {
reject("Not implemented for iOS");
} catch (ex) {
console.log("Error in mapbox.setTilt: " + ex);
reject(ex);
}
});
};
mapbox.getTilt = function () {
return new Promise(function (resolve, reject) {
try {
reject("Not implemented for iOS");
} catch (ex) {
console.log("Error in mapbox.getTilt: " + ex);
reject(ex);
}
});
};
mapbox.animateCamera = function (arg) {
return new Promise(function (resolve, reject) {
try {
var target = arg.target;
if (target === undefined) {
reject("Please set the 'target' parameter");
return;
}
var cam = MGLMapCamera.camera();
cam.centerCoordinate = CLLocationCoordinate2DMake(target.lat, target.lng);
if (arg.altitude) {
cam.altitude = arg.altitude;
}
if (arg.bearing) {
cam.heading = arg.bearing;
}
if (arg.tilt) {
cam.pitch = arg.tilt;
}
var duration = arg.duration ? (arg.duration / 1000) : 10;
mapbox.mapView.setCameraWithDurationAnimationTimingFunction(
cam,
duration,
CAMediaTimingFunction.functionWithName(kCAMediaTimingFunctionEaseInEaseOut));
resolve();
} catch (ex) {
console.log("Error in mapbox.animateCamera: " + ex);
reject(ex);
}
});
};
mapbox.addPolygon = function (arg) {
return new Promise(function (resolve, reject) {
try {
var points = arg.points;
if (points === undefined) {
reject("Please set the 'points' parameter");
return;
}
/*
TODO 'sizeof' is not valid in {N}, but we need it for this:
var coordinates = malloc(points.length * sizeof(CLLocationCoordinate2D));
for (var i=0; i<points.length; i++) {
var point = points[i];
coordinates[i] = CLLocationCoordinate2DMake(point.lat, point.lng);
}
var polygon = MGLPolygon.polygonWithCoordinatesCount(
coordinates,
points.length);
mapbox.mapView.addAnnotation(polygon);
*/
reject("not implemented for iOS (yet)");
} catch (ex) {
console.log("Error in mapbox.addPolygon: " + ex);
reject(ex);
}
});
};
mapbox._reportOfflineRegionDownloadProgress = function() {
if (firebase._receivedNotificationCallback !== null) {
for (var p in firebase._pendingNotifications) {
var userInfoJSON = firebase._pendingNotifications[p];
console.log("Received a push notification with title: " + userInfoJSON.aps.alert.title);
// move the most relevant properties so it's according to the TS definition and aligned with Android
userInfoJSON.title = userInfoJSON.aps.alert.title;
userInfoJSON.body = userInfoJSON.aps.alert.body;
userInfoJSON.badge = userInfoJSON.aps.badge;
firebase._receivedNotificationCallback(userInfoJSON);
}
firebase._pendingNotifications = [];
firebase._addObserver(kFIRInstanceIDTokenRefreshNotification, firebase._onTokenRefreshNotification);
UIApplication.sharedApplication().applicationIconBadgeNumber = 0;
}
};
mapbox.getViewport = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (!mapbox.mapView) {
reject("No map has been loaded");
return;
}
var visibleBounds = mapbox.mapView.visibleCoordinateBounds;
var bounds = {
north: visibleBounds.ne.latitude,
east: visibleBounds.ne.longitude,
south: visibleBounds.sw.latitude,
west: visibleBounds.sw.longitude
};
resolve({
bounds: bounds,
zoomLevel: mapbox.mapView.zoomLevel
});
} catch (ex) {
console.log("Error in mapbox.getViewport: " + ex);
reject(ex);
}
});
};
mapbox.deleteOfflineRegion = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (!arg || !arg.name) {
reject("Pass in the 'region' param");
return;
}
var packs = MGLOfflineStorage.sharedOfflineStorage().packs;
var regions = [];
var found = false;
for (var i = 0; i < packs.count; i++) {
var pack = packs.objectAtIndex(i);
var userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context);
var name = userInfo.objectForKey("name");
if (name === arg.name) {
found = true;
MGLOfflineStorage.sharedOfflineStorage().removePackWithCompletionHandler(pack, function(p, error) {
if (error) {
console.log("del error: " + error);
console.log("del error: " + error.localizedFailureReason);
// The pack couldn’t be deleted for some reason.
reject(error.localizedFailureReason);
} else {
resolve();
// don't return, see note below
}
});
// don't break the loop as there may be multiple packs with the same name
}
}
if (!found) {
reject("Region not found");
}
} catch (ex) {
console.log("Error in mapbox.deleteOfflineRegion: " + ex);
reject(ex);
}
});
};
mapbox.listOfflineRegions = function (arg) {
return new Promise(function (resolve, reject) {
try {
var packs = MGLOfflineStorage.sharedOfflineStorage().packs;
if (!packs) {
reject("No packs found or Mapbox not ready yet");
return;
}
var regions = [];
for (var i = 0; i < packs.count; i++) {
var pack = packs.objectAtIndex(i);
var region = pack.region;
var style = region.styleURL;
var userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(pack.context);
regions.push({
name: userInfo.objectForKey("name"),
style: "" + region.styleURL,
minZoom: region.minimumZoomLevel,
maxZoom: region.maximumZoomLevel,
bounds: {
north: region.bounds.ne.latitude,
east: region.bounds.ne.longitude,
south: region.bounds.sw.latitude,
west: region.bounds.sw.longitude
}
});
}
resolve(regions);
} catch (ex) {
console.log("Error in mapbox.listOfflineRegions: " + ex);
reject(ex);
}
});
};
mapbox.downloadOfflineRegion = function (arg) {
return new Promise(function (resolve, reject) {
try {
// TODO verify input of all params, and mark them mandatory in TS d.
var styleURL = mapbox._getMapStyle(arg.style);
var swCoordinate = CLLocationCoordinate2DMake(arg.bounds.south, arg.bounds.west);
var neCoordinate = CLLocationCoordinate2DMake(arg.bounds.north, arg.bounds.east);
var bounds = MGLCoordinateBounds;
bounds.sw = swCoordinate;
bounds.ne = neCoordinate;
var region = MGLTilePyramidOfflineRegion.alloc().initWithStyleURLBoundsFromZoomLevelToZoomLevel(
styleURL,
bounds,
arg.minZoom,
arg.maxZoom);
// TODO there's more observers, see https://www.mapbox.com/ios-sdk/examples/offline-pack/
if (arg.onProgress) {
mapbox._addObserver(MGLOfflinePackProgressChangedNotification, function (notification) {
var offlinePack = notification.object;
var offlinePackProgress = offlinePack.progress;
var userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(offlinePack.context);
var complete = offlinePackProgress.countOfResourcesCompleted == offlinePackProgress.countOfResourcesExpected;
arg.onProgress({
name: userInfo.objectForKey("name"),
completed: offlinePackProgress.countOfResourcesCompleted,
expected: offlinePackProgress.countOfResourcesExpected,
percentage: Math.round((offlinePackProgress.countOfResourcesCompleted / offlinePackProgress.countOfResourcesExpected) * 10000) / 100,
complete: complete
});
if (complete) {
resolve();
}
});
}
mapbox._addObserver(MGLOfflinePackErrorNotification, function (notification) {
var offlinePack = notification.object;
var userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(offlinePack.context);
var error = notification.userInfo[MGLOfflinePackErrorUserInfoKey];
reject({
name: userInfo.objectForKey("name"),
error: "Download error. " + error
});
});
mapbox._addObserver(MGLOfflinePackMaximumMapboxTilesReachedNotification, function (notification) {
var offlinePack = notification.object;
var userInfo = NSKeyedUnarchiver.unarchiveObjectWithData(offlinePack.context);
var maximumCount = notification.userInfo[MGLOfflinePackMaximumCountUserInfoKey];
console.log("Offline region " + userInfo.objectForKey("name") + " reached the tile limit of " + maximumCount);
});
// Store some data for identification purposes alongside the downloaded resources.
var userInfo = {"name": arg.name };
var context = NSKeyedArchiver.archivedDataWithRootObject(userInfo);
// Create and register an offline pack with the shared offline storage object.
MGLOfflineStorage.sharedOfflineStorage().addPackForRegionWithContextCompletionHandler(region, context, function(pack, error) {
if (error) {
console.log("addPackForRegionWithContextCompletionHandler error: " + error);
console.log("addPackForRegionWithContextCompletionHandler error.localizedFailureReason: " + error.localizedFailureReason);
// The pack couldn’t be created for some reason.
reject(error);
} else {
// Start downloading.
pack.resume();
}
});
} catch (ex) {
console.log("Error in mapbox.downloadOfflineRegion: " + ex);
reject(ex);
}
});
};
mapbox._addObserver = function (eventName, callback) {
return NSNotificationCenter.defaultCenter().addObserverForNameObjectQueueUsingBlock(eventName, null, NSOperationQueue.mainQueue(), callback);
};
var MGLMapViewDelegateImpl = (function (_super) {
__extends(MGLMapViewDelegateImpl, _super);
function MGLMapViewDelegateImpl() {
_super.apply(this, arguments);
}
MGLMapViewDelegateImpl.new = function () {
return _super.new.call(this);
};
MGLMapViewDelegateImpl.prototype.initWithCallback = function (mapLoadedCallback) {
this._mapLoadedCallback = mapLoadedCallback;
return this;
};
MGLMapViewDelegateImpl.prototype.mapViewDidFinishLoadingMap = function(mapView) {
this._mapLoadedCallback();
};
MGLMapViewDelegateImpl.prototype.mapViewAnnotationCanShowCallout = function(mapView, annotation) {
return true;
};
// fired when the marker icon is about to be rendered - return null for the default icon
MGLMapViewDelegateImpl.prototype.mapViewImageForAnnotation = function(mapView, annotation) {
var cachedMarker = _getTappedMarkerDetails(annotation);
if (cachedMarker && cachedMarker.iconPath) {
if (cachedMarker.reuseIdentifier) {
return mapView.dequeueReusableAnnotationImageWithIdentifier(cachedMarker.reuseIdentifier);
}
var appPath = fs.knownFolders.currentApp().path;
var iconFullPath = appPath + "/" + cachedMarker.iconPath;
if (fs.File.exists(iconFullPath)) {
var image = imgSrc.fromFile(iconFullPath).ios;
// TODO (future) add resize options for nice retina rendering
cachedMarker.reuseIdentifier = cachedMarker.iconPath;
return MGLAnnotationImage.annotationImageWithImageReuseIdentifier(image, cachedMarker.reuseIdentifier);
}
}
return null;
};
// fired when on of the callout's accessoryviews is tapped (not currently used)
MGLMapViewDelegateImpl.prototype.mapViewAnnotationCalloutAccessoryControlTapped = function(mapView, annotation, control) {
};
// fired when a marker is tapped
MGLMapViewDelegateImpl.prototype.mapViewDidSelectAnnotation = function(mapView, annotation) {
var cachedMarker = _getTappedMarkerDetails(annotation);
if (cachedMarker && cachedMarker.onTap) {
cachedMarker.onTap(cachedMarker);
}
};
// fired when a callout is tapped
MGLMapViewDelegateImpl.prototype.mapViewTapOnCalloutForAnnotation = function(mapView, annotation) {
var cachedMarker = _getTappedMarkerDetails(annotation);
if (cachedMarker && cachedMarker.onCalloutTap) {
cachedMarker.onCalloutTap(cachedMarker);
}
};
function _getTappedMarkerDetails(tapped) {
for (var m in mapbox._markers) {
var cached = mapbox._markers[m];
if (cached.lat == tapped.coordinate.latitude &&
cached.lng == tapped.coordinate.longitude &&
cached.title == tapped.title &&
cached.subtitle == tapped.subtitle) {
return cached;
}
}
}
MGLMapViewDelegateImpl.ObjCProtocols = [MGLMapViewDelegate];
return MGLMapViewDelegateImpl;
})(NSObject);
module.exports = mapbox;