Skip to content

Commit b452da4

Browse files
committedNov 13, 2020
fix: some fixes
1 parent 6a36f61 commit b452da4

File tree

3 files changed

+51
-109
lines changed

3 files changed

+51
-109
lines changed
 

‎src/mapbox.android.ts

+39-95
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
import { request } from '@nativescript-community/perms';
8-
import { AndroidApplication, Application, Color, File, Trace, Utils, knownFolders } from '@nativescript/core';
8+
import { AndroidApplication, Application, Color, File, Trace, Utils, knownFolders, path } from '@nativescript/core';
99
import { AndroidActivityBundleEventData, AndroidActivityEventData } from '@nativescript/core/application/application-interfaces';
1010
import { getImage } from '@nativescript/core/http';
1111
import { GeoUtils } from './geo.utils';
@@ -796,15 +796,15 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
796796

797797
const showIt = () => {
798798
if (Trace.isEnabled()) {
799-
CLog(CLogTypes.info, 'show(): showit() top');
799+
CLog(CLogTypes.info, 'show()');
800800
}
801801

802802
// if no accessToken was set the app may crash.
803803
//
804804
// FIXME: Even if using a local server add some string.
805805

806806
if (settings.accessToken === undefined) {
807-
reject("Please set the 'accessToken' parameter");
807+
reject('mapbox_accesstoken_missing');
808808
return;
809809
}
810810

@@ -833,10 +833,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
833833
context = settings.context;
834834
}
835835

836-
if (Trace.isEnabled()) {
837-
CLog(CLogTypes.info, 'show(): before getInstance()');
838-
}
839-
840836
// Per the Mapbox Android Native samples:
841837
//
842838
// "Mapbox access token is configured here. This needs to be called either in your application
@@ -859,34 +855,24 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
859855
// define some listeners to inform in case the map does not
860856
// load.
861857

862-
this.onDidFailLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener({
863-
onDidFailLoadingMap: (error) => {
864-
console.error('Mapbox::show(): failed to load map:', error);
865-
},
866-
});
867-
868858
if (Trace.isEnabled()) {
869-
CLog(CLogTypes.info, 'show(): about on add onDidFailLoadingMapListener:', this.onDidFailLoadingMapListener);
859+
this.onDidFailLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener({
860+
onDidFailLoadingMap: (error) => CLog(CLogTypes.error, 'Mapbox::show(): failed to load map:', error),
861+
});
862+
this._mapboxViewInstance.addOnDidFailLoadingMapListener(this.onDidFailLoadingMapListener);
870863
}
871864

872-
this._mapboxViewInstance.addOnDidFailLoadingMapListener(this.onDidFailLoadingMapListener);
873-
874865
// this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener', this.onDidFailLoadingMapListener);
875866

876867
if (Trace.isEnabled()) {
877868
this.onDidFinishLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingMapListener({
878869
onDidFinishLoadingMap: () => CLog(CLogTypes.info, 'show(): finished loading map'),
879870
});
871+
this._mapboxViewInstance.addOnDidFinishLoadingMapListener(this.onDidFinishLoadingMapListener);
880872
}
881873

882-
this._mapboxViewInstance.addOnDidFinishLoadingMapListener(this.onDidFinishLoadingMapListener);
883-
884874
// this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingMapListener', this.onDidFinishLoadingMapListener);
885875

886-
if (Trace.isEnabled()) {
887-
CLog(CLogTypes.info, 'show(): after adding fail listener()');
888-
}
889-
890876
this.onMapReadyCallback = new com.mapbox.mapboxsdk.maps.OnMapReadyCallback({
891877
onMapReady: (mbMap) => {
892878
this._mapboxMapInstance = mbMap;
@@ -899,10 +885,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
899885
// is initialized. We do not consider the map ready until the style has successfully
900886
// loaded.
901887

902-
if (Trace.isEnabled()) {
903-
CLog(CLogTypes.info, "Mapbox::show(): attempting to set style '" + settings.style);
904-
}
905-
906888
this.setMapStyle(settings.style).then((style) => {
907889
if (Trace.isEnabled()) {
908890
CLog(CLogTypes.info, 'show(): style loaded.');
@@ -1709,7 +1691,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17091691
}
17101692

17111693
// callback for when the style is successfully loaded.
1712-
17131694
this.onDidFinishLoadingStyleListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFinishLoadingStyleListener({
17141695
onDidFinishLoadingStyle: () => {
17151696
if (Trace.isEnabled()) {
@@ -1743,7 +1724,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17431724
// this.gcFix('com.mapbox.mapboxsdk.plugins.annotation.OnAnnotationClickListener', this.onAnnotationClickListener);
17441725

17451726
resolve();
1746-
},
1727+
}
17471728
});
17481729

17491730
this._mapboxViewInstance.addOnDidFinishLoadingStyleListener(this.onDidFinishLoadingStyleListener);
@@ -1755,30 +1736,26 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17551736
this.onDidFailLoadingMapListener = new com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener({
17561737
onDidFailLoadingMap: (error) => {
17571738
if (Trace.isEnabled()) {
1758-
CLog(CLogTypes.info, 'Mapbox:setMapStyle(): style failed');
1739+
CLog(CLogTypes.error, 'Mapbox:setMapStyle(): style failed', mapStyle, error);
17591740
}
17601741
reject(error);
17611742
},
17621743
});
17631744

1764-
if (Trace.isEnabled()) {
1765-
CLog(CLogTypes.info, 'setMapStyle(): before onDidFailLoadingMapListener');
1766-
}
1767-
17681745
this._mapboxViewInstance.addOnDidFailLoadingMapListener(this.onDidFailLoadingMapListener);
17691746

17701747
// this.gcFix('com.mapbox.mapboxsdk.maps.MapView.OnDidFailLoadingMapListener', this.onDidFailLoadingMapListener);
17711748

17721749
const builder = new com.mapbox.mapboxsdk.maps.Style.Builder();
17731750

1774-
this._mapboxMapInstance.setStyle(builder.fromUrl(mapStyle));
1751+
this._mapboxMapInstance.setStyle(builder.fromUri(mapStyle));
17751752

17761753
// FIXME: probably not necessary.
17771754

17781755
// this.gcFix('com.mapbox.mapboxsdk.maps.Style.Builder', builder);
17791756
} catch (ex) {
17801757
if (Trace.isEnabled()) {
1781-
CLog(CLogTypes.info, 'Error in mapbox.setMapStyle: ' + ex);
1758+
CLog(CLogTypes.error, 'Error in mapbox.setMapStyle', style, ex);
17821759
}
17831760
reject(ex);
17841761
}
@@ -1787,34 +1764,28 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17871764

17881765
// ------------------------------------------------------------------------------
17891766

1790-
addMarkers(markers: MapboxMarker[], nativeMap?: any): Promise<void> {
1791-
return new Promise((resolve, reject) => {
1792-
try {
1793-
this._addMarkers(markers, this._mapboxViewInstance);
1794-
resolve();
1795-
} catch (ex) {
1796-
if (Trace.isEnabled()) {
1797-
CLog(CLogTypes.info, 'Error in mapbox.addMarkers: ' + ex);
1798-
}
1799-
reject(ex);
1767+
async addMarkers(markers: MapboxMarker[], nativeMap?: any) {
1768+
try {
1769+
this._addMarkers(markers, this._mapboxViewInstance);
1770+
} catch (ex) {
1771+
if (Trace.isEnabled()) {
1772+
CLog(CLogTypes.info, 'Error in mapbox.addMarkers: ' + ex);
18001773
}
1801-
});
1774+
throw (ex);
1775+
}
18021776
}
18031777

18041778
// ----------------------------------------------------------------------------------
18051779

1806-
removeMarkers(ids?: any, nativeMap?: any): Promise<void> {
1807-
return new Promise((resolve, reject) => {
1808-
try {
1809-
this._removeMarkers(ids, this._mapboxViewInstance);
1810-
resolve();
1811-
} catch (ex) {
1812-
if (Trace.isEnabled()) {
1813-
CLog(CLogTypes.info, 'Error in mapbox.removeMarkers: ' + ex);
1814-
}
1815-
reject(ex);
1780+
async removeMarkers(ids?: any, nativeMap?: any) {
1781+
try {
1782+
this._removeMarkers(ids, this._mapboxViewInstance);
1783+
} catch (ex) {
1784+
if (Trace.isEnabled()) {
1785+
CLog(CLogTypes.info, 'Error in mapbox.removeMarkers: ' + ex);
18161786
}
1817-
});
1787+
throw (ex);
1788+
}
18181789
}
18191790

18201791
// --------------------------------------------------------------------------------------------
@@ -3858,31 +3829,25 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
38583829

38593830
const Style = com.mapbox.mapboxsdk.maps.Style;
38603831

3861-
if (Trace.isEnabled()) {
3862-
CLog(CLogTypes.info, '_getMapStyle(): Style object is:', Style);
3863-
}
3864-
38653832
// allow for a style URL to be passed
38663833

3867-
if (/^mapbox:\/\/styles/.test(input) || /^http:\/\//.test(input) || /^https:\/\//.test(input)) {
3868-
return input;
3869-
} else if (/^~\//.test(input)) {
3870-
const assetsPath = 'asset://app/';
3871-
input = input.replace(/^~\//, assetsPath);
3834+
if (input.startsWith('mapbox://styles') || input.startsWith('http://') || input.startsWith('https://')) {
38723835
return input;
3873-
} else if (input === MapStyle.LIGHT || input === MapStyle.LIGHT.toString()) {
3836+
} else if (input.startsWith('~/')) {
3837+
return 'file://' + path.join(knownFolders.currentApp().path, input.replace('~/', ''));
3838+
} else if (input === MapStyle.LIGHT) {
38743839
return Style.LIGHT;
3875-
} else if (input === MapStyle.DARK || input === MapStyle.DARK.toString()) {
3840+
} else if (input === MapStyle.DARK) {
38763841
return Style.DARK;
3877-
} else if (input === MapStyle.OUTDOORS || input === MapStyle.OUTDOORS.toString()) {
3842+
} else if (input === MapStyle.OUTDOORS) {
38783843
return Style.OUTDOORS;
3879-
} else if (input === MapStyle.SATELLITE || input === MapStyle.SATELLITE.toString()) {
3844+
} else if (input === MapStyle.SATELLITE) {
38803845
return Style.SATELLITE;
3881-
} else if (input === MapStyle.SATELLITE_STREETS || input === MapStyle.SATELLITE_STREETS.toString()) {
3846+
} else if (input === MapStyle.SATELLITE_STREETS) {
38823847
return Style.SATELLITE_STREETS;
3883-
} else if (input === MapStyle.TRAFFIC_DAY || input === MapStyle.TRAFFIC_DAY.toString()) {
3848+
} else if (input === MapStyle.TRAFFIC_DAY) {
38843849
return Style.TRAFFIC_DAY;
3885-
} else if (input === MapStyle.TRAFFIC_NIGHT || input === MapStyle.TRAFFIC_NIGHT.toString()) {
3850+
} else if (input === MapStyle.TRAFFIC_NIGHT) {
38863851
return Style.TRAFFIC_NIGHT;
38873852
} else {
38883853
// default
@@ -4041,7 +4006,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
40414006
return new Promise((resolve, reject) => {
40424007
try {
40434008
if (Trace.isEnabled()) {
4044-
CLog(CLogTypes.info, 'showUserLocationMarker(): top');
4009+
CLog(CLogTypes.info, 'showUserLocationMarker()');
40454010
}
40464011

40474012
if (!this._mapboxMapInstance) {
@@ -4074,21 +4039,12 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
40744039

40754040
const componentOptions = componentOptionsBuilder.build();
40764041

4077-
if (Trace.isEnabled()) {
4078-
CLog(CLogTypes.info, 'showUserLocationMarker(): after componentOptions.build()');
4079-
}
40804042

40814043
this._locationComponent = this._mapboxMapInstance.getLocationComponent();
40824044

4083-
if (Trace.isEnabled()) {
4084-
CLog(CLogTypes.info, 'showUserLocationMarker(): after getLocationComponent');
4085-
}
40864045

40874046
const activationOptionsBuilder = com.mapbox.mapboxsdk.location.LocationComponentActivationOptions.builder(Application.android.context, this._mapboxMapInstance.getStyle());
40884047

4089-
if (Trace.isEnabled()) {
4090-
CLog(CLogTypes.info, 'showUserLocationMarker(): after activationOptionsBuilder');
4091-
}
40924048

40934049
activationOptionsBuilder.locationComponentOptions(componentOptions);
40944050

@@ -4097,23 +4053,11 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
40974053
if (typeof options.useDefaultLocationEngine != 'undefined') {
40984054
useDefaultEngine = options.useDefaultLocationEngine;
40994055
}
4100-
4101-
if (Trace.isEnabled()) {
4102-
CLog(CLogTypes.info, 'showUserLocationMarker(): before useDefaultEngine');
4103-
}
4104-
41054056
activationOptionsBuilder.useDefaultLocationEngine(useDefaultEngine);
41064057

4107-
if (Trace.isEnabled()) {
4108-
CLog(CLogTypes.info, 'showUserLocationMarker(): after useDefaultEngine');
4109-
}
41104058

41114059
const locationComponentActivationOptions = activationOptionsBuilder.build();
41124060

4113-
if (Trace.isEnabled()) {
4114-
CLog(CLogTypes.info, 'showUserLocationMarker(): after ActivationOptions');
4115-
}
4116-
41174061
this._locationComponent.activateLocationComponent(locationComponentActivationOptions);
41184062
this._locationComponent.setLocationComponentEnabled(true);
41194063

‎src/mapbox.common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export interface MapboxMarker extends LatLng {
129129
icon?: string;
130130
/**
131131
* The preferred way is using the 'icon' property, but you can still reference a local file directly.
132-
* Example: "res/markers/green_pin_marker.png"
132+
* Example: "~/assets/markers/green_pin_marker.png"
133133
*/
134134
iconPath?: string;
135135
/**

‎src/mapbox.ios.ts

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Color, File, ImageSource, Trace, Utils, knownFolders } from '@nativescript/core';
1+
import { Color, File, ImageSource, Trace, Utils, knownFolders, path } from '@nativescript/core';
22

33
import {
44
AddExtrusionOptions,
@@ -73,25 +73,23 @@ const _setMapboxMapOptions = (mapView: MGLMapView, settings) => {
7373
};
7474

7575
const _getMapStyle = (input: any): NSURL => {
76-
if (/^mapbox:\/\/styles/.test(input) || /^http:\/\//.test(input) || /^https:\/\//.test(input)) {
76+
if (input.startsWith('mapbox://styles') || input.startsWith('http://') || input.startsWith('https://')) {
7777
return NSURL.URLWithString(input);
78-
} else if (/^~\//.test(input)) {
79-
const assetPath = 'file://' + knownFolders.currentApp().path + '/';
80-
input = input.replace(/^~\//, assetPath);
81-
return NSURL.URLWithString(input);
82-
} else if (input === MapStyle.LIGHT || input === MapStyle.LIGHT.toString()) {
78+
} else if (input.startsWith('~/')) {
79+
return NSURL.URLWithString( 'file://' + path.join(knownFolders.currentApp().path, input.replace('~/', '')));
80+
} else if (input === MapStyle.LIGHT) {
8381
return MGLStyle.lightStyleURL;
84-
} else if (input === MapStyle.DARK || input === MapStyle.DARK.toString()) {
82+
} else if (input === MapStyle.DARK) {
8583
return MGLStyle.darkStyleURL;
86-
} else if (input === MapStyle.OUTDOORS || input === MapStyle.OUTDOORS.toString()) {
84+
} else if (input === MapStyle.OUTDOORS) {
8785
return MGLStyle.outdoorsStyleURL;
88-
} else if (input === MapStyle.SATELLITE || input === MapStyle.SATELLITE.toString()) {
86+
} else if (input === MapStyle.SATELLITE) {
8987
return MGLStyle.satelliteStyleURL;
90-
} else if (input === MapStyle.SATELLITE_STREETS || input === MapStyle.SATELLITE_STREETS.toString()) {
88+
} else if (input === MapStyle.SATELLITE_STREETS) {
9189
return MGLStyle.satelliteStreetsStyleURL;
92-
} else if (input === MapStyle.TRAFFIC_DAY || input === MapStyle.TRAFFIC_DAY.toString()) {
90+
} else if (input === MapStyle.TRAFFIC_DAY) {
9391
return NSURL.URLWithString('mapbox://styles/mapbox/traffic-day-v2');
94-
} else if (input === MapStyle.TRAFFIC_NIGHT || input === MapStyle.TRAFFIC_NIGHT.toString()) {
92+
} else if (input === MapStyle.TRAFFIC_NIGHT) {
9593
return NSURL.URLWithString('mapbox://styles/mapbox/traffic-night-v2');
9694
} else {
9795
return MGLStyle.streetsStyleURL;

0 commit comments

Comments
 (0)