Skip to content

Commit 2a83615

Browse files
committed
fix(android): fixed map init from content view
1 parent 3d4cedd commit 2a83615

File tree

2 files changed

+7201
-5591
lines changed

2 files changed

+7201
-5591
lines changed

src/ui-mapbox/index.android.ts

Lines changed: 24 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ export class MapboxView extends MapboxViewBase {
175175
* programmatically include settings
176176
*/
177177
setConfig(settings: ShowOptions) {
178+
if (Trace.isEnabled()) {
179+
CLog(CLogTypes.info, 'setConfig()');
180+
}
178181
// zoom level is not applied unless center is set
179-
180182
if (settings.zoomLevel && !settings.center) {
181183
// Eiffel tower, Paris
182184
settings.center = {
@@ -296,14 +298,15 @@ export class MapboxView extends MapboxViewBase {
296298
* @todo FIXME: this.nativeMapView is unused and never actually set to anything.
297299
*/
298300
async initMap() {
301+
const accessToken = this.config?.accessToken ?? this.settings?.accessToken
299302
if (Trace.isEnabled()) {
300-
CLog(CLogTypes.info, "MapboxView:initMap(): top - accessToken is '" + this.config.accessToken + "'", this.config);
303+
CLog(CLogTypes.info, "MapboxView:initMap(): top - accessToken is '" + accessToken + "'", this.config);
301304
}
302-
if (!this.config.accessToken) {
305+
if (!accessToken) {
303306
throw new Error('missing accessToken');
304307
}
305308

306-
if (!this.nativeMapView && ((this.config && this.config.accessToken) || (this.settings && this.settings.accessToken))) {
309+
if (!this.nativeMapView) {
307310
this.mapbox = new Mapbox(this);
308311
// the NativeScript contentview class extends from Observable to provide the notify method
309312
// which is the glue that joins this code with whatever callbacks are set in the Mapbox XML
@@ -1683,7 +1686,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
16831686
const features = result.getValue();
16841687
const jsFeatures = [];
16851688
for (let i = 0; i < features.size(); i++) {
1686-
const feature = features.get(i) as com.mapbox.maps.QueriedRenderedFeature;
1689+
const feature = features.get(i);
16871690
jsFeatures.push(JSON.parse(feature.getQueriedFeature().getFeature().toJson()));
16881691
}
16891692
resolve(jsFeatures);
@@ -1732,7 +1735,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
17321735
const features = result.getValue();
17331736
const jsFeatures = [];
17341737
for (let i = 0; i < features.size(); i++) {
1735-
const feature = features.get(i) as com.mapbox.maps.QueriedSourceFeature;
1738+
const feature = features.get(i);
17361739
jsFeatures.push(JSON.parse(feature.getQueriedFeature().getFeature().toJson()));
17371740
}
17381741
resolve(jsFeatures);
@@ -2423,7 +2426,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
24232426
const regions = [];
24242427
if (offlineRegions !== null) {
24252428
for (let i = 0; i < offlineRegions.size(); i++) {
2426-
const offlineRegion = offlineRegions.get(i) as com.mapbox.common.TileRegion;
2429+
const offlineRegion = offlineRegions.get(i);
24272430
// const bounds = offlineRegionDefinition.getBounds();
24282431
const { bounds, maxZoom, minZoom, name, regionId, styleUrl, ...metadata } = await this._getRegionMetadata(offlineRegion);
24292432

@@ -2476,7 +2479,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
24762479
let found = false;
24772480
if (offlineRegions !== null) {
24782481
for (let i = 0; i < offlineRegions.size(); i++) {
2479-
const offlineRegion = offlineRegions.get(i) as com.mapbox.common.TileRegion;
2482+
const offlineRegion = offlineRegions.get(i);
24802483
const regionId = options.id ? offlineRegion.getId() : await this._getRegionName(offlineRegion);
24812484
if (regionId === (options.id || options.name)) {
24822485
found = true;
@@ -2898,7 +2901,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
28982901
if (lineFeatures.size() === 0) {
28992902
reject(new Error('no line string feature found'));
29002903
} else {
2901-
const feature = (lineFeatures.get(0) as com.mapbox.maps.QueriedSourceFeature).getQueriedFeature().getFeature();
2904+
const feature = lineFeatures.get(0).getQueriedFeature().getFeature();
29022905

29032906
const newPoints = new java.util.ArrayList<com.mapbox.geojson.Point>((feature.geometry() as com.mapbox.geojson.LineString).coordinates());
29042907
newPoints.add(com.mapbox.geojson.Point.fromLngLat(lnglat[0], lnglat[1]));
@@ -3018,24 +3021,15 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
30183021
CLog(CLogTypes.info, 'trackUser(): top');
30193022
}
30203023

3021-
return new Promise((resolve, reject) => {
3024+
return new Promise(async (resolve, reject) => {
30223025
try {
30233026
if (!this._mapboxMapInstance) {
30243027
reject('No map has been loaded');
30253028
return;
30263029
}
30273030

3028-
this.requestFineLocationPermission()
3029-
.then(() => {
3030-
// if (this._locationComponent) {
3031-
// this.changeUserLocationMarkerMode(options.renderMode || 'COMPASS', options.cameraMode || 'TRACKING');
3032-
// } else {
3033-
this.showUserLocationMarker(options as any);
3034-
// }
3035-
})
3036-
.catch((err) => {
3037-
console.error('Location permission denied. error:', err);
3038-
});
3031+
await this.requestFineLocationPermission();
3032+
this.showUserLocationMarker(options, nativeMap);
30393033

30403034
resolve();
30413035
} catch (ex) {
@@ -3280,11 +3274,13 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
32803274
* @todo at least with simulated data, the location is only updated once hence adding support for forceLocation method.
32813275
*/
32823276
async showUserLocationMarker(
3283-
options: TrackUserOptions & {
3284-
accuracyColor;
3285-
accuracyRingColor;
3286-
pulsingColor;
3287-
},
3277+
options: Partial<
3278+
TrackUserOptions & {
3279+
accuracyColor?;
3280+
accuracyRingColor?;
3281+
pulsingColor?;
3282+
}
3283+
>,
32883284
nativeMap?
32893285
) {
32903286
try {
@@ -3348,88 +3344,6 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
33483344
}
33493345
})
33503346
);
3351-
3352-
// if (typeof options.elevation != 'undefined') {
3353-
// componentOptionsBuilder = componentOptionsBuilder.elevation(options.elevation);
3354-
// }
3355-
3356-
// if (typeof options.accuracyAlpha != 'undefined') {
3357-
// componentOptionsBuilder = componentOptionsBuilder.accuracyAlpha(options.accuracyAlpha);
3358-
// }
3359-
3360-
// if (typeof options.foregroundTintColor != 'undefined') {
3361-
// const foregroundTintColor = new java.lang.Integer(android.graphics.Color.parseColor(options.foregroundTintColor));
3362-
// componentOptionsBuilder = componentOptionsBuilder.foregroundTintColor(foregroundTintColor);
3363-
// }
3364-
3365-
// if (typeof options.foregroundStaleTintColor != 'undefined') {
3366-
// const foregroundStaleTintColor = new java.lang.Integer(android.graphics.Color.parseColor(options.foregroundStaleTintColor));
3367-
// componentOptionsBuilder = componentOptionsBuilder.foregroundStaleTintColor(foregroundStaleTintColor);
3368-
// }
3369-
3370-
// if (typeof options.backgroundTintColor != 'undefined') {
3371-
// const backgroundTintColor = new java.lang.Integer(android.graphics.Color.parseColor(options.backgroundTintColor));
3372-
// componentOptionsBuilder = componentOptionsBuilder.backgroundTintColor(backgroundTintColor);
3373-
// }
3374-
3375-
// if (typeof options.bearingTintColor != 'undefined') {
3376-
// const bearingTintColor = new java.lang.Integer(android.graphics.Color.parseColor(options.bearingTintColor));
3377-
// componentOptionsBuilder = componentOptionsBuilder.bearingTintColor(bearingTintColor);
3378-
// }
3379-
3380-
// const componentOptions = componentOptionsBuilder.build();
3381-
3382-
// this._locationComponent = this._mapboxMapInstance.getLocationComponent();
3383-
3384-
// const activationOptionsBuilder = com.mapbox.maps.location.LocationComponentActivationOptions.builder(Utils.android.getApplicationContext(), this._mapboxMapInstance.getStyle());
3385-
3386-
// activationOptionsBuilder.locationComponentOptions(componentOptions);
3387-
3388-
// let useDefaultEngine = true;
3389-
3390-
// if (typeof options.useDefaultLocationEngine != 'undefined') {
3391-
// useDefaultEngine = options.useDefaultLocationEngine;
3392-
// }
3393-
// activationOptionsBuilder.useDefaultLocationEngine(useDefaultEngine);
3394-
3395-
// const locationComponentActivationOptions = activationOptionsBuilder.build();
3396-
3397-
// this._locationComponent.activateLocationComponent(locationComponentActivationOptions);
3398-
// this._locationComponent.setLocationComponentEnabled(true);
3399-
3400-
// let cameraMode = this._stringToCameraMode('TRACKING');
3401-
3402-
// if (typeof options.cameraMode != 'undefined') {
3403-
// cameraMode = this._stringToCameraMode(options.cameraMode);
3404-
// }
3405-
3406-
// this._locationComponent.setCameraMode(cameraMode);
3407-
3408-
// let renderMode = com.mapbox.maps.location.modes.RenderMode.COMPASS;
3409-
3410-
// if (typeof options.renderMode != 'undefined') {
3411-
// renderMode = this._stringToRenderMode(options.renderMode);
3412-
// }
3413-
3414-
// this._locationComponent.setRenderMode(renderMode);
3415-
3416-
// if (Trace.isEnabled()) {
3417-
// CLog(CLogTypes.info, 'showUserLocationMarker(): after renderMode');
3418-
// }
3419-
3420-
// if (typeof options.clickListener != 'undefined') {
3421-
// this.onLocationClickListener = new com.mapbox.maps.location.OnLocationClickListener({
3422-
// onLocationComponentClick: () => {
3423-
// options.clickListener();
3424-
// }
3425-
// });
3426-
3427-
// this._locationComponent.addOnLocationClickListener(this.onLocationClickListener);
3428-
// }
3429-
3430-
// if (typeof options.cameraTrackingChangedListener != 'undefined') {
3431-
// this._locationComponent.addOnCameraTrackingChangedListener(options.cameraTrackingChangedListener);
3432-
// }
34333347
} catch (ex) {
34343348
if (Trace.isEnabled()) {
34353349
CLog(CLogTypes.info, 'Error in mapbox.showUserLocationMarker: ' + ex);
@@ -3488,7 +3402,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
34883402
return new Promise((resolve, reject) => {
34893403
try {
34903404
if (Trace.isEnabled()) {
3491-
CLog(CLogTypes.info, 'showUserLocationMarker()');
3405+
CLog(CLogTypes.info, 'changeUserLocationMarkerMode()');
34923406
}
34933407

34943408
if (!this._mapboxMapInstance) {
@@ -3617,7 +3531,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
36173531
const result: Layer[] = [];
36183532

36193533
for (let i = 0; i < layers.size(); i++) {
3620-
const id = (layers.get(i) as com.mapbox.maps.StyleObjectInfo).getId();
3534+
const id = layers.get(i).getId();
36213535
//TODO: fix declaration which is missing extension for this
36223536
//@ts-ignore
36233537
result.push(new Layer(style.getLayer(id)));

0 commit comments

Comments
 (0)