nativescript-mapbox-enduco
Version:
Native Maps, by Mapbox.
1,021 lines • 137 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mapbox = exports.MapboxView = exports.BundleKludge = exports.MapStyle = void 0;
var utils = require("tns-core-modules/utils/utils");
var application = require("tns-core-modules/application");
var fs = require("tns-core-modules/file-system");
var color_1 = require("tns-core-modules/color");
var http = require("tns-core-modules/http");
var location_1 = require("nativescript-advanced-permissions/location");
var mapbox_common_1 = require("./mapbox.common");
Object.defineProperty(exports, "MapStyle", { enumerable: true, get: function () { return mapbox_common_1.MapStyle; } });
var geo_utils_1 = require("./geo.utils");
var image_source_1 = require("tns-core-modules/image-source");
var ACCESS_FINE_LOCATION_PERMISSION_REQUEST_CODE = 111;
var BundleKludge;
(function (BundleKludge) {
BundleKludge.bundle = { test: 'test' };
})(BundleKludge = exports.BundleKludge || (exports.BundleKludge = {}));
var MapboxView = (function (_super) {
__extends(MapboxView, _super);
function MapboxView() {
var _this = _super.call(this) || this;
_this.settings = null;
_this.initialized = false;
console.log("MapboxView::constructor(): building new MapboxView object.");
_this.gcFixInit();
return _this;
}
MapboxView.prototype.setConfig = function (settings) {
if (settings.zoomLevel && !settings.center) {
settings.center = {
lat: 48.858093,
lng: 2.294694
};
}
this.settings = settings;
};
MapboxView.prototype.gcFixInit = function () {
if (typeof global['MapboxView'] == 'undefined') {
this.gcFixIndex = 0;
global['MapboxView'] = [
{}
];
}
else {
global['MapboxView'].push({});
this.gcFixIndex = global['MapboxView'].length - 1;
}
console.log("MapboxView::gcFixInit(): index is:", this.gcFixIndex);
};
MapboxView.prototype.gcFix = function (key, ref) {
global['MapboxView'][this.gcFixIndex][key] = ref;
};
MapboxView.prototype.gcClear = function () {
global['MapboxView'][this.gcFixIndex] = null;
};
MapboxView.prototype.getNativeMapView = function () {
return this.nativeMapView;
};
MapboxView.prototype.getMapboxApi = function () {
return this.mapbox;
};
MapboxView.prototype.createNativeView = function () {
console.log("MapboxView:createNativeView(): top");
var nativeView = new android.widget.FrameLayout(this._context);
this.gcFix('android.widget.FrameLayout', nativeView);
console.log("MapboxView:createNativeView(): bottom");
return nativeView;
};
MapboxView.prototype.initNativeView = function () {
var _this = this;
console.log("MapboxView::initNativeView(): top");
this.nativeView.owner = this;
_super.prototype.initNativeView.call(this);
this.on('loaded', function () {
console.log("MapboxView::initNativeView(): on - loaded");
if (!_this.initialized) {
_this.initMap();
_this.initialized = true;
}
});
this.on('unloaded', function () {
console.log("MapboxView::initNativeView(): on - unloaded");
});
console.log("MapboxView::initNativeView(): bottom");
};
MapboxView.prototype.disposeNativeView = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
console.log("MapboxView::disposeNativeView(): top");
this.nativeView.owner = null;
return [4, this.mapbox.destroy()];
case 1:
_a.sent();
console.log("MapboxView::disposeNativeView(): after mapbox.destroy()");
this.gcClear();
_super.prototype.disposeNativeView.call(this);
console.log("MapboxView::disposeNativeView(): bottom");
return [2];
}
});
});
};
MapboxView.prototype.initMap = function () {
var _this = this;
console.log("MapboxView:initMap(): top - accessToken is '" + this.config.accessToken + "'", this.config);
if (!this.nativeMapView && (this.config.accessToken || this.settings.accessToken)) {
this.mapbox = new Mapbox();
var options = {
context: this._context,
parentView: this.nativeView,
onLocationPermissionGranted: function (event) {
_this.notify({
eventName: mapbox_common_1.MapboxViewBase.locationPermissionGrantedEvent,
object: _this,
map: _this,
android: _this.nativeMapView
});
},
onLocationPermissionDenied: function (event) {
_this.notify({
eventName: mapbox_common_1.MapboxViewBase.locationPermissionDeniedEvent,
object: _this,
map: _this,
android: _this.nativeMapView
});
},
onMapReady: function (map) {
console.log("MapboxView::initMap(): onMapReady event - calling notify with the MapboxViewBase.mapReadyEvent");
if (_this.hasListeners(mapbox_common_1.MapboxViewBase.mapReadyEvent)) {
console.log("MapboxView::initMap(): onMapReady has listeners.");
}
else {
console.log("MapboxView::initMap(): onMapReady DOES NOT HAVE listeners.");
}
_this.notify({
eventName: mapbox_common_1.MapboxViewBase.mapReadyEvent,
object: _this,
map: _this,
android: _this.nativeMapView
});
},
onScrollEvent: function (event) {
console.log("MapboxView::initMap(): onScrollEvent event");
_this.notify({
eventName: mapbox_common_1.MapboxViewBase.scrollEvent,
object: _this,
event: event,
map: _this,
android: _this.nativeMapView
});
},
onMoveBeginEvent: function (event) {
console.log("MapboxView::initMap(): onMoveBeginEvent event");
_this.notify({
eventName: mapbox_common_1.MapboxViewBase.moveBeginEvent,
object: _this,
event: event,
map: _this,
android: _this.nativeMapView
});
}
};
console.log("MapboxView::initMap(): this.config is:", this.config);
if (!this.settings) {
this.settings = Mapbox.merge(this.config, Mapbox.defaults);
}
else {
this.settings = Mapbox.merge(this.settings, Mapbox.defaults);
}
this.settings = Mapbox.merge(this.settings, options);
console.log("MapboxView::initMap(): before show.");
this.mapbox.show(this.settings);
console.log("MapboxView::initMap(): bottom.");
}
};
MapboxView.prototype.onDisableScrollChanged = function (oldValue, newValue) {
if (this.mapbox) {
this.getMapboxApi().setScrollingEnabled(!newValue);
}
};
return MapboxView;
}(mapbox_common_1.MapboxViewBase));
exports.MapboxView = MapboxView;
var Mapbox = (function (_super) {
__extends(Mapbox, _super);
function Mapbox() {
var _this = _super.call(this) || this;
_this._locationComponent = false;
_this._permissionsManager = false;
_this._accessToken = '';
_this.circleManager = null;
_this.lineManager = null;
_this.symbolManager = null;
_this._markers = [];
_this._polylines = [];
_this._polygons = [];
_this.circles = [];
_this.lines = [];
_this.eventCallbacks = [];
_this._markerIconDownloadCache = [];
console.log("Mapbox::constructor(): building new Mapbox object.");
_this.eventCallbacks['click'] = [];
_this._activity = application.android.foregroundActivity;
application.android.on(application.AndroidApplication.activityStartedEvent, function (args) {
console.log("Mapbox::constructor: activityStartedEvent Event: " + args.eventName + ", Activity: " + args.activity);
if (_this._mapboxViewInstance && _this._activity === args.activity) {
console.log("Mapbox::constructor(): calling onStart()");
_this._mapboxViewInstance.onStart();
}
});
application.android.on(application.AndroidApplication.activityPausedEvent, function (args) {
console.log("Mapbox::constructor:: activityPausedEvent Event: " + args.eventName + ", Activity: " + args.activity);
if (_this._mapboxViewInstance && _this._activity === args.activity) {
console.log("Mapbox::constructor(): calling onPause()");
_this._mapboxViewInstance.onPause();
}
});
application.android.on(application.AndroidApplication.activityResumedEvent, function (args) {
console.log("Mapbox::constructor: activityResumedEvent Event: " + args.eventName + ", Activity: " + args.activity);
if (_this._mapboxViewInstance && _this._activity === args.activity) {
console.log("Mapbox::constructor(): calling onResume() - destroyed flag is:", _this._mapboxViewInstance.isDestroyed());
_this._mapboxViewInstance.onResume();
}
});
application.android.on(application.AndroidApplication.activityStoppedEvent, function (args) {
console.log("Mapbox::constructor: activityStoppedEvent Event: " + args.eventName + ", Activity: " + args.activity);
if (_this._mapboxViewInstance && _this._activity === args.activity) {
console.log("Mapbox::constructor(): calling onStop()");
_this._mapboxViewInstance.onStop();
}
});
application.android.on(application.AndroidApplication.activityDestroyedEvent, function (args) {
console.log("Mapbox::constructor: activityDestroyedEvent Event: " + args.eventName + ", Activity: " + args.activity);
if (_this._activity === args.activity) {
if (_this.lineManager) {
_this.lineManager.onDestroy();
}
}
if (_this.circleManager) {
_this.circleManager.onDestroy();
}
if (_this.symbolManager) {
_this.symbolManager.onDestroy();
}
if (_this._mapboxViewInstance) {
console.log("Mapbox::constructor(): calling onDestroy()");
_this._mapboxViewInstance.onDestroy();
}
});
application.android.on(application.AndroidApplication.saveActivityStateEvent, function (args) {
console.log("Mapbox::constructor: saveActivityStateEvent Event: " + args.eventName + ", Activity: " + args.activity + ", Bundle: " + args.bundle);
if (_this._mapboxViewInstance && _this._activity === args.activity) {
console.log("Mapbox::constructor(): saving instance state");
_this._mapboxViewInstance.onSaveInstanceState(args.bundle);
}
});
application.android.on(application.AndroidApplication.activityResultEvent, function (args) {
console.log("Mapbox::constructor: activityResultEvent Event: " + args.eventName + ", Activity: " + args.activity +
", requestCode: " + args.requestCode + ", resultCode: " + args.resultCode + ", Intent: " + args.intent);
});
application.android.on(application.AndroidApplication.activityBackPressedEvent, function (args) {
console.log("Mapbox::constructor: activityBackPressedEvent Event: " + args.eventName + ", Activity: " + args.activity);
});
console.log("Mapbox::constructor(): end of Mapbox constructor.");
return _this;
}
Mapbox.prototype.gcFix = function (key, ref) {
if (typeof global['Mapbox'] == 'undefined') {
global['Mapbox'] = {};
}
global['Mapbox'][key] = ref;
};
Mapbox.prototype.gcClear = function () {
global['Mapbox'] = {};
};
Mapbox.prototype.setMapboxViewInstance = function (mapboxViewInstance) {
};
Mapbox.prototype.setMapboxMapInstance = function (mapboxMapInstance) {
};
Mapbox.prototype.show = function (options) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var settings_1 = Mapbox.merge(options, Mapbox.defaults);
var showIt = function () {
console.log("Mapbox::show(): showit() top");
if (settings_1.accessToken === undefined) {
reject("Please set the 'accessToken' parameter");
return;
}
if (_this._mapboxViewInstance) {
console.log("Mapbox::show(): view already created. Removing it.");
var viewGroup = _this._mapboxViewInstance.getParent();
if (viewGroup !== null) {
console.log("Mapbox::show(): view already created. Removing _mapboxViewInstance child of view parent.");
viewGroup.removeView(_this._mapboxViewInstance);
}
}
_this._accessToken = settings_1.accessToken;
var context = application.android.context;
if (settings_1.context) {
context = settings_1.context;
}
console.log("Mapbox::show(): before getInstance()");
com.mapbox.mapboxsdk.Mapbox.getInstance(context, _this._accessToken);
var mapboxMapOptions = _this._getMapboxMapOptions(settings_1);
_this._mapboxViewInstance = new com.mapbox.mapboxsdk.maps.MapView(context, mapboxMapOptions);
_this._mapboxViewInstance.onCreate(null);
_this.onDidFailLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener({
onDidFailLoadingMap: function (error) {
console.error("Mapbox::show(): failed to load map:", error);
}
});
console.log("Mapbox::show(): about on add onDidFailLoadingMapListener:", _this.onDidFailLoadingMapListener);
_this._mapboxViewInstance.addOnDidFailLoadingMapListener(_this.onDidFailLoadingMapListener);
_this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener', _this.onDidFailLoadingMapListener);
_this.onDidFinishLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingMapListener({
onDidFinishLoadingMap: function (map) {
console.log("Mapbox::show(): finished loading map:");
}
});
_this._mapboxViewInstance.addOnDidFinishLoadingMapListener(_this.onDidFinishLoadingMapListener);
_this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingMapListener', _this.onDidFinishLoadingMapListener);
console.log("Mapbox::show(): after adding fail listener()");
_this.onMapReadyCallback = new com.mapbox.mapboxsdk.maps.OnMapReadyCallback({
onMapReady: function (mbMap) {
_this._mapboxMapInstance = mbMap;
console.log("Mapbox::show(): onMapReady() with instance:", _this._mapboxMapInstance);
console.log("Mapbox::show(): attempting to set style '" + settings_1.style);
_this.setMapStyle(settings_1.style).then(function (style) {
console.log("Mapbox::show(): style loaded.");
_this.initEventHandlerShim(settings_1, _this._mapboxViewInstance);
_this._addMarkers(settings_1.markers, _this._mapboxViewInstance);
if (settings_1.showUserLocation) {
_this.requestFineLocationPermission().then(function () {
_this.showUserLocationMarker({});
if (settings_1.onLocationPermissionGranted) {
settings_1.onLocationPermissionGranted(_this._mapboxMapInstance);
}
}).catch(function (err) {
if (settings_1.onLocationPermissionDenied) {
settings_1.onLocationPermissionDenied(_this._mapboxMapInstance);
}
});
}
if (settings_1.onMapReady) {
settings_1.onMapReady(_this._mapboxMapInstance);
}
resolve({
android: _this._mapboxViewInstance
});
});
}
});
_this._mapboxViewInstance.getMapAsync(_this.onMapReadyCallback);
_this.gcFix('com.mapbox.mapboxsdk.maps.OnMapReadyCallback', _this.onMapReadyCallback);
console.log("Mapbox::show(): after getMapAsync()");
if (settings_1.parentView) {
console.log("Mapbox::show(): adding map to passed in view");
settings_1.parentView.addView(_this._mapboxViewInstance);
}
else if (settings_1.container) {
console.log("Mapbox::show(): adding map to passed in container");
context = application.android.foregroundActivity;
if (!context) {
context = application.android.startActivity;
}
var mapViewLayout = new android.widget.FrameLayout(context);
console.log("Mapbox::show(): before adding mapboxViewInstance to FrameLayout");
mapViewLayout.addView(_this._mapboxViewInstance);
console.log("Mapbox::show(): before adding FrameLayout to container");
settings_1.container.addChild(mapViewLayout);
}
console.log("Mapbox::show(): showIt() bottom");
};
setTimeout(showIt, settings_1.delay ? settings_1.delay : 200);
}
catch (ex) {
console.log("Error in mapbox.show: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.hide = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
if (_this._mapboxViewInstance) {
var viewGroup = _this._mapboxViewInstance.getParent();
if (viewGroup !== null) {
viewGroup.setVisibility(android.view.View.INVISIBLE);
}
}
resolve();
}
catch (ex) {
console.log("Error in mapbox.hide: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.unhide = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
if (_this._mapboxViewInstance) {
_this._mapboxViewInstance.getParent().setVisibility(android.view.View.VISIBLE);
resolve();
}
else {
reject("No map found");
}
}
catch (ex) {
console.log("Error in mapbox.unhide: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.destroy = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var viewGroup;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.clearEventListeners();
this.gcClear();
console.log("Mapbox::destroy(): destroying mapbox view.");
if (this.lineManager) {
this.lineManager.onDestroy();
this.lineManager = null;
}
if (this.circleManager) {
this.circleManager.onDestroy();
this.circleManager = null;
}
if (this.symbolManager) {
this.symbolManager.onDestroy();
this.symbolManager = null;
}
if (!this._locationComponent) return [3, 2];
console.log("Mapbox::destroy(): Location marker not disabled before destroy() called.");
return [4, this.hideUserLocationMarker()];
case 1:
_a.sent();
_a.label = 2;
case 2:
if (this._mapboxViewInstance) {
viewGroup = this._mapboxViewInstance.getParent();
if (viewGroup !== null) {
console.log("Mapbox::destroy(): removing _mapboxViewInstance view.");
viewGroup.removeView(this._mapboxViewInstance);
}
this._mapboxViewInstance.onPause();
this._mapboxViewInstance.onStop();
this._mapboxViewInstance.destroyDrawingCache();
this._mapboxViewInstance.onDestroy();
this._mapboxViewInstance = null;
this._mapboxMapInstance = null;
}
resolve();
return [2];
}
});
}); });
};
Mapbox.prototype.clearEventListeners = function () {
if (this.onDidFailLoadingMapListener) {
this._mapboxViewInstance.removeOnDidFailLoadingMapListener(this.onDidFailLoadingMapListener);
}
if (this.onDidFinishLoadingMapListener) {
this._mapboxViewInstance.removeOnDidFinishLoadingMapListener(this.onDidFinishLoadingMapListener);
}
if (this.onDidFinishLoadingStyleListener) {
this._mapboxViewInstance.removeOnDidFinishLoadingStyleListener(this.onDidFinishLoadingStyleListener);
}
if (this.onAnnotationClickListener) {
this.lineManager.removeClickListener(this.onAnnotationClickListener);
}
if (this.onDidFailLoadingMapListener) {
this._mapboxViewInstance.removeOnDidFailLoadingMapListener(this.onDidFailLoadingMapListener);
}
if (this.onMapClickListener) {
this._mapboxMapInstance.removeOnMapClickListener(this.onMapClickListener);
}
if (this.onMapLongClickListener) {
this._mapboxMapInstance.removeOnMapLongClickListener(this.onMapLongClickListener);
}
if (this.onMoveListener) {
this._mapboxMapInstance.removeOnMoveListener(this.onMoveListener);
}
if (this.onScrollListener) {
this._mapboxMapInstance.removeOnMoveListener(this.onScrollListener);
}
if (this.onFlingListener) {
this._mapboxMapInstance.removeOnFlingListener(this.onFlingListener);
}
if (this.onCameraMoveListener) {
this._mapboxMapInstance.removeOnCameraMoveListener(this.onCameraMoveListener);
}
if (this.onCameraMoveCancelListener) {
this._mapboxMapInstance.removeOnCameraMoveCancelListener(this.onCameraMoveCancelListener);
}
if (this.onCameraIdleListener) {
this._mapboxMapInstance.removeOnCameraIdleListener(this.onCameraIdleListener);
}
if (this.onLocationClickListener) {
this._locationComponent.removeOnLocationClickListener(this.onLocationClickListener);
}
};
Mapbox.prototype.onStart = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._mapboxViewInstance.onStart();
resolve();
});
};
Mapbox.prototype.onResume = function (nativeMapViewInstance) {
var _this = this;
return new Promise(function (resolve, reject) {
console.log("Mapbox::onResume(): calling resume");
_this._mapboxViewInstance.onResume();
console.log("Mapbox::onResume(): after calling resume on nativeMap:", _this._mapboxViewInstance);
resolve();
});
};
Mapbox.prototype.onPause = function (nativeMapViewInstance) {
var _this = this;
return new Promise(function (resolve, reject) {
console.log("Mapbox::onPause(): calling pause");
_this._mapboxViewInstance.onPause();
resolve();
});
};
Mapbox.prototype.onStop = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._mapboxViewInstance.onStop();
resolve();
});
};
Mapbox.prototype.onLowMemory = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._mapboxViewInstance.onLowMemory();
resolve();
});
};
Mapbox.prototype.onDestroy = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
_this._mapboxViewInstance.onDestroy();
resolve();
});
};
Mapbox.prototype.initEventHandlerShim = function (settings, mapboxNativeViewInstance) {
var _this = this;
console.log("Mapbox:initEventHandlerShim(): top");
this.setOnMapClickListener(function (point) {
return _this.checkForCircleClickEvent(point);
}, mapboxNativeViewInstance);
this.setOnMoveBeginListener(function (point) {
console.log("Mapbox:initEventHandlerShim(): moveBegin:", point);
if (typeof settings.onMoveBeginEvent != 'undefined') {
settings.onMoveBeginEvent(point);
}
}, mapboxNativeViewInstance);
};
Mapbox.prototype.onMapEvent = function (eventName, id, callback, nativeMapView) {
var _this = this;
if (typeof this.eventCallbacks[eventName] == 'undefined') {
this.eventCallbacks[eventName] = [];
}
var lineEntry = this.lines.find(function (entry) {
return entry.id == id;
});
if (lineEntry) {
console.log("Mapbox:on(): we have a line entry:", lineEntry);
this.addClickableLineOverlay(lineEntry.id, nativeMapView)
.then(function (clickOverlay) {
lineEntry.clickOverlay = clickOverlay;
console.log("Mapbox:on(): pushing id '" + id + "' with clickOverlay:", clickOverlay);
_this.eventCallbacks[eventName].push({
id: id,
callback: callback
});
});
}
else {
this.eventCallbacks[eventName].push({
id: id,
callback: callback
});
}
};
Mapbox.prototype.offMapEvent = function (eventName, id, nativeMapView) {
if (typeof this.eventCallbacks[eventName] == 'undefined') {
return;
}
this.eventCallbacks[eventName] = this.eventCallbacks[eventName].filter(function (entry) {
return entry.id != id;
});
};
Mapbox.prototype.handleLineClickEvent = function (clickOverlay) {
var lineEntry = this.lines.find(function (entry) {
console.log("Mapbox:handleLineClickEvent(): checking lineEntry clickOverlay id '" + entry.clickOverlay + "' against clickOverlay '" + clickOverlay + "'");
return entry.clickOverlay == clickOverlay;
});
if (!lineEntry) {
console.error("Mapbox:handleLineClick(): click on overlay without an underlying line layer");
return false;
}
for (var x = 0; x < this.eventCallbacks['click'].length; x++) {
var entry = this.eventCallbacks['click'][x];
console.log("Mapbox:handleLineClickEvent(): checking entry id '" + entry.id + "' against lineEnty id '" + lineEntry.id + "'");
if (entry.id == lineEntry.id) {
console.log("Mapbox:handleLineClickEvent(): calling callback for '" + entry.id + "'");
return entry.callback(lineEntry);
}
}
return false;
};
Mapbox.prototype.checkForCircleClickEvent = function (point) {
console.log("Mapbox:checkForCircleClickEvent(): got click event with point:", point);
for (var i = 0; i < this.circles.length; i++) {
console.log("Mapbox:checkForCircleClickEvent(): checking circle with radius:", this.circles[i].radius);
if (geo_utils_1.GeoUtils.isLocationInCircle(point.lng, point.lat, this.circles[i].center[0], this.circles[i].center[1], this.circles[i].radius)) {
console.log("Mapbox:checkForCircleClickEvent() Point is in circle with id '" + this.circles[i].id + "' invoking callbacks, if any. Callback list is:", this.eventCallbacks);
for (var x = 0; x < this.eventCallbacks['click'].length; x++) {
var entry = this.eventCallbacks['click'][x];
if (entry.id == this.circles[i].id) {
console.log("Mapbox:checkForCircleClickEvent(): calling callback for '" + entry.id + "'");
return entry.callback(point);
}
}
}
}
return false;
};
Mapbox.prototype.addClickableLineOverlay = function (lineId, nativeMapView) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var lineEntry = _this.lines.find(function (entry) {
return entry.id == lineId;
});
if (!lineEntry) {
reject("No such line with id '" + lineId + "'");
return;
}
var width = lineEntry.layer.getLineWidth().getValue();
console.log("Mapbox:addClickableLineOverlay(): we have a source line of width '" + width + "'");
var feature = lineEntry.feature;
console.log("Mapbox:addClickableLineOverlay(): after removing properties");
feature.addNumberProperty('line-opacity', new java.lang.Float(0));
feature.addNumberProperty('line-width', width);
console.log("Mapbox:addClickableLineOverlay(): after updating feature");
var featureCollection = new com.mapbox.geojson.FeatureCollection.fromFeature(feature);
_this.gcFix('com.mapbox.geojson.FeatureCollection', featureCollection);
var clickOverlay = _this.lineManager.create(featureCollection).get(0);
console.log("Mapbox:addClickableLineOverlay(): after creating overlay:", clickOverlay);
resolve(clickOverlay);
}
catch (ex) {
console.log("MapboxaddClickableLineOverlay error: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.hasFineLocationPermission = function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
resolve(_this._fineLocationPermissionGranted());
}
catch (ex) {
console.log("Error in mapbox.hasFineLocationPermission: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.requestFineLocationPermission = function () {
return new Promise(function (resolve, reject) {
console.log("Mapbox::requestFineLocationPermission()");
if (location_1.hasLocationPermissions()) {
resolve();
return;
}
if (!location_1.isLocationEnabled) {
console.error("Location Services are not turned on");
reject("Location services are not turned on.");
return;
}
;
location_1.requestLocationPermissions(true, "We use this permission to show you your current location.").then(function (hasPermission) {
console.log("Mapbox::requestFineLocationPermission(): hasPermission is:", hasPermission);
if (location_1.hasLocationPermissions()) {
console.log("Mapbox::requestFineLocationPermission(): permission granted.");
resolve(true);
return;
}
else {
console.error("Location Permission not granted.");
reject("Location Permission Not granted");
return;
}
;
});
});
};
Mapbox.prototype.onRequestPermissionsResults = function (requestCode, permissions, grantResults) {
console.log("Mapbox::onRequestPermissionsResult()");
this._permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
};
Mapbox.prototype.setMapStyle = function (style, nativeMapViewInstance) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var mapStyle = _this._getMapStyle(style);
console.log("Mapbox::setMapStyle(): with style:", style);
_this.onDidFinishLoadingStyleListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingStyleListener({
onDidFinishLoadingStyle: function (style) {
console.log("Mapbox:setMapStyle(): style loaded");
_this.lineManager = new com.mapbox.mapboxsdk.plugins.annotation.LineManager(_this._mapboxViewInstance, _this._mapboxMapInstance, _this._mapboxMapInstance.getStyle());
_this.gcFix('lineManager', _this.lineManager);
_this.onAnnotationClickListener = new com.mapbox.mapboxsdk.plugins.annotation.OnAnnotationClickListener({
onAnnotationClick: function (line) {
console.log("Mapbox:setMapStyle(): click on line:", line);
_this.handleLineClickEvent(line);
return true;
}
});
_this.lineManager.addClickListener(_this.onAnnotationClickListener);
_this.gcFix('com.mapbox.mapboxsdk.plugins.annotation.OnAnnotationClickListener', _this.onAnnotationClickListener);
resolve(style);
}
});
_this._mapboxViewInstance.addOnDidFinishLoadingStyleListener(_this.onDidFinishLoadingStyleListener);
_this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingStyleListener', _this.onDidFinishLoadingStyleListener);
_this.onDidFailLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener({
onDidFailLoadingMap: function (error) {
console.log("Mapbox:setMapStyle(): style failed");
reject(error);
}
});
console.log("Mapbox::setMapStyle(): before onDidFailLoadingMapListener");
_this._mapboxViewInstance.addOnDidFailLoadingMapListener(_this.onDidFailLoadingMapListener);
_this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener', _this.onDidFailLoadingMapListener);
var builder = new com.mapbox.mapboxsdk.maps.Style.Builder();
_this._mapboxMapInstance.setStyle(builder.fromUrl(mapStyle));
_this.gcFix('com.mapbox.mapboxsdk.maps.Style.Builder', builder);
}
catch (ex) {
console.log("Error in mapbox.setMapStyle: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.addMarkers = function (markers, nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this._addMarkers(markers, _this._mapboxViewInstance);
resolve();
}
catch (ex) {
console.log("Error in mapbox.addMarkers: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.removeMarkers = function (ids, nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this._removeMarkers(ids, _this._mapboxViewInstance);
resolve();
}
catch (ex) {
console.log("Error in mapbox.removeMarkers: " + ex);
reject(ex);
}
});
};
Mapbox.prototype._addMarkers = function (markers, nativeMap) {
var _this = this;
if (!markers) {
console.log("No markers passed");
return;
}
if (!Array.isArray(markers)) {
console.log("markers must be passed as an Array: [{title:'foo'}]");
return;
}
if (!this._mapboxMapInstance) {
return;
}
this._mapboxMapInstance.setOnMarkerClickListener(new com.mapbox.mapboxsdk.maps.MapboxMap.OnMarkerClickListener({
onMarkerClick: function (marker) {
var cachedMarker = _this._getClickedMarkerDetails(marker);
if (cachedMarker && cachedMarker.onTap) {
cachedMarker.onTap(cachedMarker);
}
return false;
}
}));
this._mapboxMapInstance.setOnInfoWindowClickListener(new com.mapbox.mapboxsdk.maps.MapboxMap.OnInfoWindowClickListener({
onInfoWindowClick: function (marker) {
var cachedMarker = _this._getClickedMarkerDetails(marker);
if (cachedMarker && cachedMarker.onCalloutTap) {
cachedMarker.onCalloutTap(cachedMarker);
}
return true;
}
}));
var iconFactory = com.mapbox.mapboxsdk.annotations.IconFactory.getInstance(application.android.context);
this._downloadMarkerImages(markers).then(function (updatedMarkers) {
var _loop_1 = function (m) {
var marker = updatedMarkers[m];
_this._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) {
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 density for icon ' " + marker.icon + "'. Falling back to the default icon.");
}
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 (fs.File.exists(iconFullPath)) {
markerOptions.setIcon(iconFactory.fromPath(iconFullPath));
}
else {
console.log("Marker icon not found, using the default instead. Requested full path: '\" + " + iconFullPath + "'.");
}
}
marker.android = _this._mapboxMapInstance.addMarker(markerOptions);
if (marker.selected) {
_this._mapboxMapInstance.selectMarker(marker.android);
}
marker.update = function (newSettings) {
for (var m_1 in _this._markers) {
var _marker = _this._markers[m_1];
if (marker.id === _marker.id) {
if (newSettings.onTap !== undefined) {
_marker.onTap = newSettings.onTap;
}
if (newSettings.onCalloutTap !== undefined) {
_marker.onCalloutTap = newSettings.onCalloutTap;
}
if (newSettings.title !== undefined) {
_marker.title = newSettings.title;
_marker.android.setTitle(newSettings.title);
}
if (newSettings.subtitle !== undefined) {
_marker.subtitle = newSettings.title;
_marker.android.setSnippet(newSettings.subtitle);
}
if (newSettings.lat && newSettings.lng) {
_marker.lat = newSettings.lat;
_marker.lng = newSettings.lng;
_marker.android.setPosition(new com.mapbox.mapboxsdk.geometry.LatLng(parseFloat(newSettings.lat), parseFloat(newSettings.lng)));
}
if (newSettings.selected) {
_this._mapboxMapInstance.selectMarker(_marker.android);
}
}
}
};
};
for (var m in updatedMarkers) {
_loop_1(m);
}
});
};
Mapbox.prototype._removeMarkers = function (ids, nativeMap) {
if (!this._mapboxMapInstance) {
return;
}
for (var m in this._markers) {
var marker = this._markers[m];
if (!ids || (marker && marker.id && ids.indexOf(marker.id) > -1)) {
if (marker && marker.android) {
this._mapboxMapInstance.removeAnnotation(marker.android);
}
}
}
if (ids) {
this._markers = this._markers.filter(function (marker) { return ids.indexOf(marker.id) === -1; });
}
else {
this._markers = [];
}
};
Mapbox.prototype.setCenter = function (options, nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var cameraPosition = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.target(new com.mapbox.mapboxsdk.geometry.LatLng(options.lat, options.lng))
.build();
_this.gcFix('com.mapbox.mapboxsdk.camera.CameraPosition.Builder', cameraPosition);
if (options.animated === true) {
var newCameraPosition = com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPosition);
_this._mapboxMapInstance.animateCamera(newCameraPosition, 1000, null);
_this.gcFix('com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition', newCameraPosition);
}
else {
_this._mapboxMapInstance.setCameraPosition(cameraPosition);
}
resolve();
}
catch (ex) {
console.log("Error in mapbox.setCenter: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.getCenter = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var coordinate = _this._mapboxMapInstance.getCameraPosition().target;
resolve({
lat: coordinate.getLatitude(),
lng: coordinate.getLongitude()
});
}
catch (ex) {
console.log("Error in mapbox.getCenter: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.setZoomLevel = function (options, nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var animated = options.animated === undefined || options.animated;
var level = options.level;
if (level >= 0 && level <= 20) {
var cameraUpdate = com.mapbox.mapboxsdk.camera.CameraUpdateFactory.zoomTo(level);
_this.gcFix('com.mapbox.mapboxsdk.camera.CameraUpdateFactory.zoomTo', cameraUpdate);
if (animated) {
_this._mapboxMapInstance.easeCamera(cameraUpdate);
}
else {
_this._mapboxMapInstance.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.prototype.getZoomLevel = function (nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var level = _this._mapboxMapInstance.getCameraPosition().zoom;
resolve(level);
}
catch (ex) {
console.log("Error in mapbox.getZoomLevel: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.setTilt = function (options, nativeMap) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
var tilt = options.tilt ? options.tilt : 30;
var cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder()
.tilt(tilt);
_this.gcFix('com.mapbox.mapboxsdk.camera.CameraPosition.Builder', cameraPositionBuilder);
var cameraUpdate = com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build());
_this.gcFix('com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition', cameraUpdate);
var durationMs = options.duration ? options.duration : 5000;
_this._mapboxMapInstance.easeCamera(cameraUpdate, durationMs);
setTimeout(function () {
resolve();
}, durationMs);
}
catch (ex) {
console.log("Error in mapbox.setTilt: " + ex);
reject(ex);
}
});
};
Mapbox.prototype.getTilt = function (nativeMap) {
var _this = this;
retu