forked from Yermo/nativescript-mapbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapbox.android.js
executable file
·1131 lines (994 loc) · 37 KB
/
mapbox.android.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var utils = require("utils/utils");
var application = require("application");
var frame = require("ui/frame");
var fs = require("file-system");
var Color = require("color").Color;
var http = require("http");
var mapbox = require("./mapbox-common");
mapbox._markers = [];
mapbox._polylines = [];
mapbox._markerIconDownloadCache = [];
var ACCESS_FINE_LOCATION_PERMISSION_REQUEST_CODE = 111; // irrelevant really, since we simply invoke onPermissionGranted
mapbox.locationServices = null;
/*************** XML definition START ****************/
var Mapbox = (function (_super) {
global.__extends(Mapbox, _super);
function Mapbox() {
_super.call(this);
this.config = {};
}
Mapbox.prototype._createUI = function () {
var context = application.android.currentContext;
this._android = new android.widget.FrameLayout(context);
var that = this;
var createUI = function() {
var settings = mapbox.merge(that.config, mapbox.defaults);
if (settings.accessToken === undefined) {
setTimeout(function() {
var dialogs = require("ui/dialogs");
dialogs.alert("Please set the 'accessToken' property because now there will be no map :)");
}, 0);
return;
}
com.mapbox.mapboxsdk.Mapbox.getInstance(application.android.context, settings.accessToken);
var drawMap = function() {
that.mapView = new com.mapbox.mapboxsdk.maps.MapView(
application.android.context,
mapbox._getMapboxMapOptions(settings));
that.mapView.onCreate(null);
that.mapView.getMapAsync(
new com.mapbox.mapboxsdk.maps.OnMapReadyCallback({
onMapReady: function (mbMap) {
that.mapView.mapboxMap = mbMap;
if (settings.showUserLocation) {
mapbox.requestFineLocationPermission().then(function() {
mapbox._showLocation(that.mapView, that);
});
}
that.notifyMapReady();
}
})
);
that._android.addView(that.mapView);
};
setTimeout(drawMap, settings.delay);
};
// postpone drawing the map for cases where this plugin is used in an Angular-powered app
setTimeout(createUI, 0);
};
Object.defineProperty(Mapbox.prototype, "android", {
get: function () {
return this._android;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Mapbox.prototype, "native", {
get: function () {
return this.mapView;
},
enumerable: true,
configurable: true
});
return Mapbox;
}(mapbox.Mapbox));
mapbox.Mapbox = Mapbox;
/*************** XML definition END ****************/
mapbox._getMapboxMapOptions = function (settings) {
var resourcename = "mapbox_mylocation_icon_default";
var res = utils.ad.getApplicationContext().getResources();
var identifier = res.getIdentifier(resourcename, "drawable", utils.ad.getApplication().getPackageName());
var iconDrawable = android.support.v4.content.ContextCompat.getDrawable(application.android.context, identifier);
var mapboxMapOptions = new com.mapbox.mapboxsdk.maps.MapboxMapOptions()
.styleUrl(mapbox._getMapStyle(settings.style))
.compassEnabled(!settings.hideCompass)
.rotateGesturesEnabled(!settings.disableRotation)
.scrollGesturesEnabled(!settings.disableScroll)
.tiltGesturesEnabled(!settings.disableTilt)
.zoomGesturesEnabled(!settings.disableZoom)
.attributionEnabled(!settings.hideAttribution)
.myLocationForegroundDrawable(iconDrawable)
// .myLocationBackgroundDrawable(iconDrawable)
.myLocationForegroundTintColor(android.graphics.Color.rgb(135, 206, 250)) // "lightskyblue"
// .myLocationBackgroundTintColor(android.graphics.Color.YELLOW)
.myLocationAccuracyTint(android.graphics.Color.rgb(135, 206, 250)) // "lightskyblue"
.myLocationAccuracyAlpha(80)
.logoEnabled(!settings.hideLogo);
// zoomlevel is not applied unless center is set
if (settings.zoomLevel && !settings.center) {
// Eiffel tower, Paris
settings.center = {
lat: 48.858093,
lng: 2.294694
}
}
if (settings.center && settings.center.lat && settings.center.lng) {
var cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.zoom(settings.zoomLevel)
.target(new com.mapbox.mapboxsdk.geometry.LatLng(settings.center.lat, settings.center.lng));
mapboxMapOptions.camera(cameraPositionBuilder.build());
}
return mapboxMapOptions;
};
mapbox._fineLocationPermissionGranted = function () {
var hasPermission = android.os.Build.VERSION.SDK_INT < 23; // Android M. (6.0)
if (!hasPermission) {
hasPermission = android.content.pm.PackageManager.PERMISSION_GRANTED ==
android.support.v4.content.ContextCompat.checkSelfPermission(application.android.foregroundActivity, android.Manifest.permission.ACCESS_FINE_LOCATION);
}
return hasPermission;
};
mapbox.hasFineLocationPermission = function () {
return new Promise(function (resolve) {
resolve(mapbox._fineLocationPermissionGranted());
});
};
mapbox.requestFineLocationPermission = function () {
return new Promise(function (resolve, reject) {
if (!mapbox._fineLocationPermissionGranted()) {
// grab the permission dialog result
application.android.on(application.AndroidApplication.activityRequestPermissionsEvent, function (args) {
for (var i = 0; i < args.permissions.length; i++) {
if (args.grantResults[i] === android.content.pm.PackageManager.PERMISSION_DENIED) {
reject("Permission denied");
return;
}
}
resolve();
});
// invoke the permission dialog
android.support.v4.app.ActivityCompat.requestPermissions(
application.android.foregroundActivity,
[android.Manifest.permission.ACCESS_FINE_LOCATION],
ACCESS_FINE_LOCATION_PERMISSION_REQUEST_CODE);
} else {
resolve();
}
});
};
mapbox._getMapStyle = function(input) {
var Style = com.mapbox.mapboxsdk.constants.Style;
// allow for a style URL to be passed
if (/^mapbox:\/\/styles/.test(input)) {
return input;
}
if (input === mapbox.MapStyle.LIGHT) {
return Style.LIGHT;
} else if (input === mapbox.MapStyle.DARK) {
return Style.DARK;
} else if (input === mapbox.MapStyle.OUTDOORS) {
return Style.OUTDOORS;
} else if (input === mapbox.MapStyle.SATELLITE) {
return Style.SATELLITE;
} else if (input === mapbox.MapStyle.HYBRID || mapbox.MapStyle.SATELLITE_STREETS) {
return Style.SATELLITE_STREETS;
} else {
// default
return Style.MAPBOX_STREETS;
}
};
mapbox._showLocation = function(theMapView, ownerObject) {
mapbox.locationServices = com.mapbox.mapboxsdk.location.LocationSource.getLocationEngine(application.android.context);
/*
var locationEngineListener = new com.mapbox.services.android.telemetry.location.LocationEngineListener({
onConnected: function () {
},
onLocationChanged: function (location) {
}
});
mapbox.locationServices.addLocationEngineListener(locationEngineListener);
*/
theMapView.mapboxMap.setMyLocationEnabled(true);
mapbox.locationServices.activate();
mapbox.locationServices.requestLocationUpdates();
};
mapbox.show = function(arg) {
return new Promise(function (resolve, reject) {
try {
var showIt = function() {
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;
}
// if already added, make sure it's removed first
if (mapbox.mapView) {
var viewGroup = mapbox.mapView.getParent();
if (viewGroup !== null) {
viewGroup.removeView(mapbox.mapView);
}
}
mapbox._accessToken = settings.accessToken;
com.mapbox.mapboxsdk.Mapbox.getInstance(application.android.context, settings.accessToken);
var mapboxMapOptions = mapbox._getMapboxMapOptions(settings);
mapbox.mapView = new com.mapbox.mapboxsdk.maps.MapView(
application.android.context,
mapboxMapOptions);
mapbox.mapView.onCreate(null);
mapbox.mapView.getMapAsync(
new com.mapbox.mapboxsdk.maps.OnMapReadyCallback({
onMapReady: function (mbMap) {
mapbox.mapboxMap = mbMap;
mapbox.mapView.mapboxMap = mapbox.mapboxMap;
// mapbox.mapboxMap.setStyleUrl(mapbox._getMapStyle(settings.style));
// mapbox.mapboxMap.setStyleUrl(com.mapbox.mapboxsdk.constants.Style.DARK);
mapbox._polylines = [];
mapbox._markers = [];
mapbox._addMarkers(settings.markers, mapbox.mapView);
if (settings.showUserLocation) {
mapbox.requestFineLocationPermission().then(function() {
mapbox._showLocation(mapbox.mapView, mapbox);
});
}
resolve();
}
})
);
// mapbox.mapView.onResume();
var topMostFrame = frame.topmost(),
density = utils.layout.getDisplayDensity(),
left = settings.margins.left * density,
right = settings.margins.right * density,
top = settings.margins.top * density,
bottom = settings.margins.bottom * density,
viewWidth = topMostFrame.currentPage.android.getWidth(),
viewHeight = topMostFrame.currentPage.android.getHeight();
var params = new android.widget.FrameLayout.LayoutParams(viewWidth - left - right, viewHeight - top - bottom);
params.setMargins(left, top, right, bottom);
mapbox.mapView.setLayoutParams(params);
if (settings.center) {
// TODO use jumpTo?
// mapbox.mapView.setCenterCoordinate(new com.mapbox.mapboxsdk.geometry.LatLng(settings.center.lat, settings.center.lng));
}
// TODO see https://github.com/mapbox/mapbox-gl-native/issues/4216
// mapbox.mapView.setZoomLevel(settings.zoomLevel);
var context = application.android.currentContext;
var mapViewLayout = new android.widget.FrameLayout(context);
mapViewLayout.addView(mapbox.mapView);
if (topMostFrame.currentPage.android.getParent()) {
topMostFrame.currentPage.android.getParent().addView(mapViewLayout);
} else {
topMostFrame.currentPage.android.addView(mapViewLayout);
}
};
// if the map is invoked immediately after launch this delay will prevent an error
setTimeout(showIt, 200);
} catch (ex) {
console.log("Error in mapbox.show: " + ex);
reject(ex);
}
});
};
mapbox._getClickedMarkerDetails = function (clicked) {
for (var m in mapbox._markers) {
var cached = mapbox._markers[m];
if (cached.lat == clicked.getPosition().getLatitude() &&
cached.lng == clicked.getPosition().getLongitude() &&
cached.title == clicked.getTitle() &&
cached.subtitle == clicked.getSnippet()) {
return cached;
}
}
};
mapbox.hide = function(arg) {
return new Promise(function (resolve, reject) {
try {
if (mapbox.mapView) {
var viewGroup = mapbox.mapView.getParent();
if (viewGroup !== null) {
viewGroup.setVisibility(android.view.View.INVISIBLE);
}
}
resolve();
} catch (ex) {
console.log("Error in mapbox.hide: " + ex);
reject(ex);
}
});
};
mapbox.unhide = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (mapbox.mapView) {
var viewGroup = mapbox.mapView.getParent();
viewGroup.setVisibility(android.view.View.VISIBLE);
resolve();
} else {
reject("No map found");
}
} catch (ex) {
console.log("Error in mapbox.unhide: " + ex);
reject(ex);
}
});
};
mapbox.destroy = function(arg) {
return new Promise(function (resolve, reject) {
if (mapbox.mapView) {
var viewGroup = mapbox.mapView.getParent();
if (viewGroup !== null) {
viewGroup.removeView(mapbox.mapView);
}
mapbox.mapView = null;
mapbox.mapboxMap = null;
}
});
};
mapbox.setMapStyle = function (style, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
theMap.mapboxMap.setStyleUrl(mapbox._getMapStyle(style));
resolve();
} catch (ex) {
console.log("Error in mapbox.setMapStyle: " + ex);
reject(ex);
}
});
};
mapbox.removeMarkers = function (ids, nativeMap) {
return new Promise(function (resolve, reject) {
try {
mapbox._removeMarkers(ids, nativeMap);
resolve();
} catch (ex) {
console.log("Error in mapbox.removeMarkers: " + ex);
reject(ex);
}
});
};
mapbox._removeMarkers = function (ids, nativeMap) {
var theMap = nativeMap || mapbox;
if (!theMap || !theMap.mapboxMap) {
return;
}
for (var m in mapbox._markers) {
var marker = mapbox._markers[m];
if (!ids || (marker.id && ids.indexOf(marker.id) > -1)) {
// don't remove the location markers in case 'removeAll' was invoked
if (ids || (marker.id != 999997 && marker.id != 999998)) {
theMap.mapboxMap.removeAnnotation(marker.android);
}
}
}
};
mapbox.addMarkers = function (markers, nativeMap) {
return new Promise(function (resolve, reject) {
try {
mapbox._addMarkers(markers, nativeMap);
resolve();
} catch (ex) {
console.log("Error in mapbox.addMarkers: " + ex);
reject(ex);
}
});
};
function downloadImage(marker) {
return new Promise(function (resolve, reject) {
// to cache..
if (mapbox._markerIconDownloadCache[marker.icon]) {
marker.iconDownloaded = mapbox._markerIconDownloadCache[marker.icon];
resolve(marker);
return;
}
// ..or not to cache
http.getImage(marker.icon).then(
function (output) {
marker.iconDownloaded = output.android;
mapbox._markerIconDownloadCache[marker.icon] = marker.iconDownloaded;
resolve(marker);
}, function (e) {
console.log("Download failed from " + marker.icon + " with error: " + e);
resolve(marker);
});
});
}
function downloadMarkerImages(markers) {
var iterations = [];
var result = [];
for (var i = 0; i < markers.length; i++) {
var marker = markers[i];
if (marker.icon && marker.icon.startsWith("http")) {
var p = downloadImage(marker).then(function(mark) {
result.push(mark);
});
iterations.push(p);
} else {
result.push(marker);
}
}
return Promise.all(iterations).then(function(output) {
return result;
});
}
mapbox._addMarkers = function(markers, nativeMap) {
if (!markers) {
console.log("No markers passed");
return;
}
if (!Array.isArray(markers)) {
console.log("markers must be passed as an Array: [{title:'foo'}]");
return;
}
var theMap = nativeMap || mapbox;
if (!theMap || !theMap.mapboxMap) {
return;
}
theMap.mapboxMap.setOnMarkerClickListener(
new com.mapbox.mapboxsdk.maps.MapboxMap.OnMarkerClickListener ({
onMarkerClick: function (marker) {
var cachedMarker = mapbox._getClickedMarkerDetails(marker);
if (cachedMarker && cachedMarker.onTap) {
cachedMarker.onTap(cachedMarker);
}
return false;
}
})
);
theMap.mapboxMap.setOnInfoWindowClickListener(
new com.mapbox.mapboxsdk.maps.MapboxMap.OnInfoWindowClickListener ({
onInfoWindowClick: function (marker) {
var cachedMarker = mapbox._getClickedMarkerDetails(marker);
if (cachedMarker && cachedMarker.onCalloutTap) {
cachedMarker.onCalloutTap(cachedMarker);
}
return true;
}
})
);
var iconFactory = com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(application.android.context);
// if any markers need to be downloaded from the web they need to be available synchronously, so fetch them first before looping
downloadMarkerImages(markers).then(function(updatedMarkers) {
for (var m in updatedMarkers) {
var marker = updatedMarkers[m];
mapbox._markers.push(marker);
var markerOptions = new com.mapbox.mapboxsdk.annotations.MarkerOptions();
markerOptions.setTitle(marker.title);
markerOptions.setSnippet(marker.subtitle);
markerOptions.setPosition(new com.mapbox.mapboxsdk.geometry.LatLng(parseFloat(marker.lat), parseFloat(marker.lng)));
if (marker.icon) {
// for markers from url see UrlMarker in https://github.com/mapbox/mapbox-gl-native/issues/5370
if (marker.icon.startsWith("res://")) {
var resourcename = marker.icon.substring(6);
var res = utils.ad.getApplicationContext().getResources();
var identifier = res.getIdentifier(resourcename, "drawable", utils.ad.getApplication().getPackageName());
if (identifier === 0) {
console.log("No icon found for this device desity for icon " + marker.icon + ", using default");
} else {
markerOptions.setIcon(iconFactory.fromResource(identifier));
}
} else if (marker.icon.startsWith("http")) {
if (marker.iconDownloaded !== null) {
markerOptions.setIcon(iconFactory.fromBitmap(marker.iconDownloaded));
}
} else {
console.log("Please use res://resourcename, http(s)://imageurl or iconPath to use a local path");
}
} else if (marker.iconPath) {
var iconFullPath = fs.knownFolders.currentApp().path + "/" + marker.iconPath;
// if the file doesn't exist the app will crash, so checking it
if (fs.File.exists(iconFullPath)) {
// could set width, height, retina, see https://github.com/Telerik-Verified-Plugins/Mapbox/pull/42/files?diff=unified&short_path=1c65267, but that's what the marker.icon param is for..
markerOptions.setIcon(iconFactory.fromPath(iconFullPath));
} else {
console.log("Marker icon not found, using the default instead. Requested full path: " + iconFullPath);
}
}
marker.android = theMap.mapboxMap.addMarker(markerOptions);
}
});
};
mapbox.setCenter = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var cameraPosition = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.target(new com.mapbox.mapboxsdk.geometry.LatLng(arg.lat, arg.lng))
.build();
if (arg.animated === true) {
theMap.mapboxMap.animateCamera(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPosition),
1000,
null);
} else {
theMap.mapboxMap.setCameraPosition(cameraPosition);
}
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.mapboxMap.getCameraPosition().target;
resolve({
lat: coordinate.getLatitude(),
lng: coordinate.getLongitude()
});
} catch (ex) {
console.log("Error in mapbox.getCenter: " + ex);
reject(ex);
}
});
};
mapbox.setZoomLevel = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var animated = arg.animated === undefined || arg.animated;
var level = arg.level;
if (level >=0 && level <= 20) {
var cameraUpdate = com.mapbox.mapboxsdk.camera.CameraUpdateFactory.zoomTo(level);
if (animated) {
theMap.mapboxMap.easeCamera(cameraUpdate);
} else {
theMap.mapboxMap.moveCamera(cameraUpdate);
}
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.mapboxMap.getCameraPosition().zoom;
resolve(level);
} catch (ex) {
console.log("Error in mapbox.getZoomLevel: " + ex);
reject(ex);
}
});
};
mapbox.setTilt = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var tilt = 30;
if (arg.tilt) {
tilt = arg.tilt;
} else if (arg.pitch) {
tilt = arg.pitch;
}
var cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.tilt(tilt);
var cameraUpdate = com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build());
theMap.mapboxMap.easeCamera(cameraUpdate, arg.duration || 5000);
resolve();
} catch (ex) {
console.log("Error in mapbox.setTilt: " + ex);
reject(ex);
}
});
};
mapbox.getTilt = function () {
return new Promise(function (resolve, reject) {
try {
var tilt = mapbox.mapboxMap.getCameraPosition().tilt;
resolve(tilt);
} catch (ex) {
console.log("Error in mapbox.getTilt: " + ex);
reject(ex);
}
});
};
mapbox.animateCamera = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var target = arg.target;
if (target === undefined) {
reject("Please set the 'target' parameter");
return;
}
var cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.target(new com.mapbox.mapboxsdk.geometry.LatLng(target.lat, target.lng));
if (arg.bearing) {
cameraPositionBuilder.bearing(arg.bearing);
}
if (arg.tilt) {
cameraPositionBuilder.tilt(arg.tilt);
}
if (arg.zoomLevel) {
cameraPositionBuilder.zoom(arg.zoomLevel);
}
theMap.mapboxMap.animateCamera(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build()),
arg.duration ? arg.duration : 10000, // default 10 seconds
null);
resolve();
} catch (ex) {
console.log("Error in mapbox.animateCamera: " + ex);
reject(ex);
}
});
};
mapbox.addPolygon = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var points = arg.points;
if (points === undefined) {
reject("Please set the 'points' parameter");
return;
}
var polygonOptions = new com.mapbox.mapboxsdk.annotations.PolygonOptions();
for (var p in points) {
var point = points[p];
polygonOptions.add(new com.mapbox.mapboxsdk.geometry.LatLng(point.lat, point.lng));
}
theMap.mapboxMap.addPolygon(polygonOptions);
resolve();
} catch (ex) {
console.log("Error in mapbox.addPolygon: " + ex);
reject(ex);
}
});
};
mapbox.addPolyline = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
var points = arg.points;
if (points === undefined) {
reject("Please set the 'points' parameter");
return;
}
var polylineOptions = new com.mapbox.mapboxsdk.annotations.PolylineOptions();
polylineOptions.width(arg.width || 5); // default 5
// Create android color && default black
var androidColor;
if (arg.color && Color.isValid(arg.color)) {
androidColor = arg.color ? new Color(arg.color).android : new Color('#000').android;
} else {
androidColor = new Color('#000').android;
}
polylineOptions.color(androidColor);
for (var p in points) {
var point = points[p];
polylineOptions.add(new com.mapbox.mapboxsdk.geometry.LatLng(point.lat, point.lng));
}
arg.android = theMap.mapboxMap.addPolyline(polylineOptions);
mapbox._polylines.push(arg);
resolve();
} catch (ex) {
console.log("Error in mapbox.addPolyline: " + ex);
reject(ex);
}
});
};
mapbox.removePolylines = function (ids, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
for (var p in mapbox._polylines) {
var polyline = mapbox._polylines[p];
if (!ids || (polyline.id && ids.indexOf(polyline.id) > -1)) {
theMap.mapboxMap.removePolyline(polyline.android);
}
}
resolve();
} catch (ex) {
console.log("Error in mapbox.removePolylines: " + ex);
reject(ex);
}
});
};
mapbox.getViewport = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (!mapbox.mapboxMap) {
reject("No map has been loaded");
return;
}
var bounds = mapbox.mapboxMap.getProjection().getVisibleRegion().latLngBounds;
resolve({
bounds: {
north: bounds.getLatNorth(),
east: bounds.getLonEast(),
south: bounds.getLatSouth(),
west: bounds.getLonWest()
},
zoomLevel: mapbox.mapboxMap.getCameraPosition().zoom
});
} catch (ex) {
console.log("Error in mapbox.getViewport: " + ex);
reject(ex);
}
});
};
mapbox.setViewport = function (arg, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
if (!theMap) {
reject("No map has been loaded");
return;
}
var bounds = new com.mapbox.mapboxsdk.geometry.LatLngBounds.Builder()
.include(new com.mapbox.mapboxsdk.geometry.LatLng(arg.bounds.north, arg.bounds.east))
.include(new com.mapbox.mapboxsdk.geometry.LatLng(arg.bounds.south, arg.bounds.west))
.build();
var animated = arg.animated === undefined || arg.animated;
var padding = 25;
theMap.mapboxMap.easeCamera(
com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newLatLngBounds(bounds, padding),
animated ? 1000 : 0);
resolve();
} catch (ex) {
console.log("Error in mapbox.setViewport: " + ex);
reject(ex);
}
});
};
mapbox.setOnMapClickListener = function (listener, nativeMap) {
return new Promise(function (resolve, reject) {
try {
var theMap = nativeMap || mapbox;
if (!theMap) {
reject("No map has been loaded");
return;
}
theMap.mapboxMap.setOnMapClickListener(
new com.mapbox.mapboxsdk.maps.MapboxMap.OnMapClickListener ({
onMapClick: function (point) {
listener({
lat: point.getLatitude(),
lng: point.getLongitude()
});
}
})
);
resolve();
} catch (ex) {
console.log("Error in mapbox.setOnMapClickListener: " + ex);
reject(ex);
}
});
};
mapbox._getRegionName = function (offlineRegion) {
var metadata = offlineRegion.getMetadata();
var jsonStr = new java.lang.String(metadata, "UTF-8");
var jsonObj = new org.json.JSONObject(jsonStr);
return jsonObj.getString("name");
};
mapbox.deleteOfflineRegion = function (arg) {
return new Promise(function (resolve, reject) {
try {
if (!arg || !arg.name) {
reject("Pass in the 'region' param");
return;
}
var pack = arg.name;
var offlineManager = mapbox._getOfflineManager();
offlineManager.listOfflineRegions(new com.mapbox.mapboxsdk.offline.OfflineManager.ListOfflineRegionsCallback({
onError: function (errorString) {
reject(errorString);
},
onList: function (offlineRegions) {
var regions = [];
var found = false;
if (offlineRegions !== null) {
for (var i=0; i< offlineRegions.length; i++) {
var offlineRegion = offlineRegions[i];
var name = mapbox._getRegionName(offlineRegion);
if (name === pack) {
found = true;
offlineRegion.delete(new com.mapbox.mapboxsdk.offline.OfflineRegion.OfflineRegionDeleteCallback({
onError: function (errorString) {
reject(errorString);
},
onDelete: function () {
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.listOfflineRegions: " + ex);
reject(ex);
}
});
};
mapbox.listOfflineRegions = function () {
return new Promise(function (resolve, reject) {
try {
var offlineManager = mapbox._getOfflineManager();
offlineManager.listOfflineRegions(new com.mapbox.mapboxsdk.offline.OfflineManager.ListOfflineRegionsCallback({
onError: function (errorString) {
reject(errorString);
},
onList: function (offlineRegions) {
var regions = [];
if (offlineRegions !== null) {
for (var i=0; i < offlineRegions.length; i++) {
var offlineRegion = offlineRegions[i];
var name = mapbox._getRegionName(offlineRegion);
var offlineRegionDefinition = offlineRegion.getDefinition();
var bounds = offlineRegionDefinition.getBounds();
regions.push({
name: name,
style: offlineRegionDefinition.getStyleURL(),
minZoom: offlineRegionDefinition.getMinZoom() ,
maxZoom: offlineRegionDefinition.getMaxZoom() ,
bounds: {
north: bounds.getLatNorth(),
east: bounds.getLonEast(),
south: bounds.getLatSouth(),
west: bounds.getLonWest()
}
});
}
}
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 bounds = new com.mapbox.mapboxsdk.geometry.LatLngBounds.Builder()
.include(new com.mapbox.mapboxsdk.geometry.LatLng(arg.bounds.north, arg.bounds.east))
.include(new com.mapbox.mapboxsdk.geometry.LatLng(arg.bounds.south, arg.bounds.west))
.build();
var retinaFactor = utils.layout.getDisplayDensity();
var offlineRegionDefinition = new com.mapbox.mapboxsdk.offline.OfflineTilePyramidRegionDefinition(
styleURL,
bounds,
arg.minZoom,
arg.maxZoom,
retinaFactor);
var info = '{name:"' + arg.name + '"}';
var infoStr = new java.lang.String(info);
var encodedMetadata = infoStr.getBytes();
if (!mapbox._accessToken && !arg.accessToken) {
reject("First show a map, or pass in an 'accessToken' param");
return;
}
var offlineManager = mapbox._getOfflineManager(arg);
offlineManager.createOfflineRegion(offlineRegionDefinition, encodedMetadata, new com.mapbox.mapboxsdk.offline.OfflineManager.CreateOfflineRegionCallback({
onError: function (errorString) {
reject(errorString);
},
onCreate: function (offlineRegion) {
if (arg.onCreate) {
arg.onCreate(offlineRegion);
}
offlineRegion.setDownloadState(com.mapbox.mapboxsdk.offline.OfflineRegion.STATE_ACTIVE);