UNPKG

nativescript-mapbox

Version:

A Nativescript plugin for Mapbox Native Maps.

1,210 lines (1,098 loc) 115 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 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"); exports.MapStyle = mapbox_common_1.MapStyle; var geo_utils_1 = require("./geo.utils"); 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; _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; } }; 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 () { var nativeView = new android.widget.FrameLayout(this._context); this.gcFix('android.widget.FrameLayout', nativeView); return nativeView; }; MapboxView.prototype.initNativeView = function () { var _this = this; this.nativeView.owner = this; _super.prototype.initNativeView.call(this); this.on('loaded', function () { if (!_this.initialized) { _this.initMap(); _this.initialized = true; } }); this.on('unloaded', function () { }); }; MapboxView.prototype.disposeNativeView = function () { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: this.nativeView.owner = null; return [4, this.mapbox.destroy()]; case 1: _a.sent(); this.gcClear(); _super.prototype.disposeNativeView.call(this); return [2]; } }); }); }; MapboxView.prototype.initMap = function () { var _this = this; 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) { if (_this.hasListeners(mapbox_common_1.MapboxViewBase.mapReadyEvent)) { } else { } _this.notify({ eventName: mapbox_common_1.MapboxViewBase.mapReadyEvent, object: _this, map: _this, android: _this.nativeMapView }); }, onScrollEvent: function (event) { _this.notify({ eventName: mapbox_common_1.MapboxViewBase.scrollEvent, object: _this, event: event, map: _this, android: _this.nativeMapView }); }, onMoveBeginEvent: function (event) { _this.notify({ eventName: mapbox_common_1.MapboxViewBase.moveBeginEvent, object: _this, event: event, map: _this, android: _this.nativeMapView }); } }; 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); this.mapbox.show(this.settings); } }; 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 = []; _this.eventCallbacks['click'] = []; _this._activity = application.android.foregroundActivity; application.android.on(application.AndroidApplication.activityStartedEvent, function (args) { if (_this._mapboxViewInstance && _this._activity === args.activity) { _this._mapboxViewInstance.onStart(); } }); application.android.on(application.AndroidApplication.activityPausedEvent, function (args) { if (_this._mapboxViewInstance && _this._activity === args.activity) { _this._mapboxViewInstance.onPause(); } }); application.android.on(application.AndroidApplication.activityResumedEvent, function (args) { if (_this._mapboxViewInstance && _this._activity === args.activity) { ((_this._mapboxViewInstance.isDestroyed()) && 0); _this._mapboxViewInstance.onResume(); } }); application.android.on(application.AndroidApplication.activityStoppedEvent, function (args) { if (_this._mapboxViewInstance && _this._activity === args.activity) { _this._mapboxViewInstance.onStop(); } }); application.android.on(application.AndroidApplication.activityDestroyedEvent, function (args) { 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) { _this._mapboxViewInstance.onDestroy(); } } }); application.android.on(application.AndroidApplication.saveActivityStateEvent, function (args) { if (_this._mapboxViewInstance && _this._activity === args.activity) { _this._mapboxViewInstance.onSaveInstanceState(args.bundle); } }); application.android.on(application.AndroidApplication.activityResultEvent, function (args) { }); application.android.on(application.AndroidApplication.activityBackPressedEvent, function (args) { }); 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 () { if (settings_1.accessToken === undefined) { reject("Please set the 'accessToken' parameter"); return; } if (_this._mapboxViewInstance) { var viewGroup = _this._mapboxViewInstance.getParent(); if (viewGroup !== null) { viewGroup.removeView(_this._mapboxViewInstance); } } _this._accessToken = settings_1.accessToken; var context = application.android.context; if (settings_1.context) { context = settings_1.context; } 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) { } }); _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) { } }); _this._mapboxViewInstance.addOnDidFinishLoadingMapListener(_this.onDidFinishLoadingMapListener); _this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingMapListener', _this.onDidFinishLoadingMapListener); _this.onMapReadyCallback = new com.mapbox.mapboxsdk.maps.OnMapReadyCallback({ onMapReady: function (mbMap) { _this._mapboxMapInstance = mbMap; _this.setMapStyle(settings_1.style).then(function (style) { _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); if (settings_1.parentView) { settings_1.parentView.addView(_this._mapboxViewInstance); } else if (settings_1.container) { context = application.android.foregroundActivity; if (!context) { context = application.android.startActivity; } var mapViewLayout = new android.widget.FrameLayout(context); mapViewLayout.addView(_this._mapboxViewInstance); settings_1.container.addChild(mapViewLayout); } }; setTimeout(showIt, settings_1.delay ? settings_1.delay : 200); } catch (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) { 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) { 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(); 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]; return [4, this.hideUserLocationMarker()]; case 1: _a.sent(); _a.label = 2; case 2: if (this._mapboxViewInstance) { viewGroup = this._mapboxViewInstance.getParent(); if (viewGroup !== null) { 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) { _this._mapboxViewInstance.onResume(); resolve(); }); }; Mapbox.prototype.onPause = function (nativeMapViewInstance) { var _this = this; return new Promise(function (resolve, reject) { _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; this.setOnMapClickListener(function (point) { return _this.checkForCircleClickEvent(point); }, mapboxNativeViewInstance); this.setOnMoveBeginListener(function (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) { this.addClickableLineOverlay(lineEntry.id, nativeMapView) .then(function (clickOverlay) { lineEntry.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) { return entry.clickOverlay == clickOverlay; }); if (!lineEntry) { return false; } for (var x = 0; x < this.eventCallbacks['click'].length; x++) { var entry = this.eventCallbacks['click'][x]; if (entry.id == lineEntry.id) { return entry.callback(lineEntry); } } return false; }; Mapbox.prototype.checkForCircleClickEvent = function (point) { for (var i = 0; i < this.circles.length; i++) { if (geo_utils_1.GeoUtils.isLocationInCircle(point.lng, point.lat, this.circles[i].center[0], this.circles[i].center[1], this.circles[i].radius)) { for (var x = 0; x < this.eventCallbacks['click'].length; x++) { var entry = this.eventCallbacks['click'][x]; if (entry.id == this.circles[i].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(); var feature = lineEntry.feature; feature.addNumberProperty('line-opacity', new java.lang.Float(0)); feature.addNumberProperty('line-width', width); var featureCollection = new com.mapbox.geojson.FeatureCollection.fromFeature(feature); _this.gcFix('com.mapbox.geojson.FeatureCollection', featureCollection); var clickOverlay = _this.lineManager.create(featureCollection).get(0); resolve(clickOverlay); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.hasFineLocationPermission = function () { var _this = this; return new Promise(function (resolve, reject) { try { resolve(_this._fineLocationPermissionGranted()); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.requestFineLocationPermission = function () { return new Promise(function (resolve, reject) { if (location_1.hasLocationPermissions()) { resolve(); return; } if (!location_1.isLocationEnabled) { 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) { if (location_1.hasLocationPermissions()) { resolve(true); return; } else { reject("Location Permission Not granted"); return; } ; }); }); }; Mapbox.prototype.onRequestPermissionsResults = function (requestCode, permissions, grantResults) { 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); _this.onDidFinishLoadingStyleListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingStyleListener({ onDidFinishLoadingStyle: function (style) { _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) { _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) { reject(error); } }); _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) { 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) { 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) { reject(ex); } }); }; Mapbox.prototype._addMarkers = function (markers, nativeMap) { var _this = this; if (!markers) { return; } if (!Array.isArray(markers)) { 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) { } else { markerOptions.setIcon(iconFactory.fromResource(identifier)); } } else if (marker.icon.startsWith("http")) { if (marker.iconDownloaded !== null) { markerOptions.setIcon(iconFactory.fromBitmap(marker.iconDownloaded)); } } else { } } else if (marker.iconPath) { var iconFullPath = fs.knownFolders.currentApp().path + "/" + marker.iconPath; if (fs.File.exists(iconFullPath)) { markerOptions.setIcon(iconFactory.fromPath(iconFullPath)); } else { } } 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) { 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) { 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) { 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) { 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) { reject(ex); } }); }; Mapbox.prototype.getTilt = function (nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { var tilt = _this._mapboxMapInstance.getCameraPosition().tilt; resolve(tilt); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.getUserLocation = function () { var _this = this; return new Promise(function (resolve, reject) { try { var loc = _this._locationComponent ? _this._locationComponent.getLastKnownLocation() : null; if (loc === null) { reject("Location not available"); } else { resolve({ location: { lat: loc.getLatitude(), lng: loc.getLongitude() }, speed: loc.getSpeed() }); } } catch (ex) { reject(ex); } }); }; Mapbox.prototype.queryRenderedFeatures = function (options, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { var point = options.point; if (point === undefined) { reject("Please set the 'point' parameter"); return; } var mapboxPoint = new com.mapbox.mapboxsdk.geometry.LatLng(options.point.lat, options.point.lng); var screenLocation = _this._mapboxMapInstance.getProjection().toScreenLocation(mapboxPoint); if (_this._mapboxMapInstance.queryRenderedFeatures) { var features = _this._mapboxMapInstance.queryRenderedFeatures(screenLocation, null, options.layerIds); var result = []; for (var i = 0; i < features.size(); i++) { var feature = features.get(i); result.push({ id: feature.id(), type: feature.type(), properties: JSON.parse(feature.properties().toString()) }); } resolve(result); } else { reject("Feature not supported by this Mapbox version"); } } catch (ex) { reject(ex); } }); }; Mapbox.prototype.addPolygon = function (options, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { var points = options.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)); } polygonOptions.fillColor(Mapbox.getAndroidColor(options.fillColor)); polygonOptions.alpha(options.fillOpacity === undefined ? 1 : options.fillOpacity); if (options.strokeColor) { polygonOptions.strokeColor(Mapbox.getAndroidColor(options.strokeColor)); } _this._polygons.push({ id: options.id || new Date().getTime(), android: _this._mapboxMapInstance.addPolygon(polygonOptions) }); resolve(); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.addPolyline = function (options, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { var points = options.points; if (points === undefined) { reject("Please set the 'points' parameter"); return; } var polylineOptions = new com.mapbox.mapboxsdk.annotations.PolylineOptions(); polylineOptions.width(options.width || 5); polylineOptions.color(Mapbox.getAndroidColor(options.color)); polylineOptions.alpha(options.opacity === undefined ? 1 : options.opacity); for (var p in points) { var point = points[p]; polylineOptions.add(new com.mapbox.mapboxsdk.geometry.LatLng(point.lat, point.lng)); } _this._polylines.push({ id: options.id || new Date().getTime(), android: _this._mapboxMapInstance.addPolyline(polylineOptions) }); resolve(); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.removePolygons = function (ids, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { for (var p in _this._polygons) { var polygon = _this._polygons[p]; if (!ids || (polygon.id && ids.indexOf(polygon.id) > -1)) { _this._mapboxMapInstance.removePolygon(polygon.android); } } resolve(); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.removePolylines = function (ids, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { for (var p in _this._polylines) { var polyline = _this._polylines[p]; if (!ids || (polyline.id && ids.indexOf(polyline.id) > -1)) { _this._mapboxMapInstance.removePolyline(polyline.android); } } resolve(); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.animateCamera = function (options, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try { var target = options.target; if (target === undefined) { reject("Please set the 'target' parameter"); return; } var cameraPositionBuilder = new com.mapbox.mapboxsdk.camera.CameraPosition.Builder(_this._mapboxMapInstance.getCameraPosition()) .target(new com.mapbox.mapboxsdk.geometry.LatLng(target.lat, target.lng)); if (options.bearing) { cameraPositionBuilder.bearing(options.bearing); } if (options.tilt) { cameraPositionBuilder.tilt(options.tilt); } if (options.zoomLevel) { cameraPositionBuilder.zoom(options.zoomLevel); } var durationMs = options.duration ? options.duration : 10000; _this._mapboxMapInstance.animateCamera(com.mapbox.mapboxsdk.camera.CameraUpdateFactory.newCameraPosition(cameraPositionBuilder.build()), durationMs, null); setTimeout(function () { resolve(); }, durationMs); } catch (ex) { reject(ex); } }); }; Mapbox.prototype.setOnMapClickListener = function (listener, nativeMap) { var _this = this; return new Promise(function (resolve, reject) { try {