Skip to content

Commit f2f4a22

Browse files
committed
fix(android): cache marker images => crazy faster addMarkers
1 parent 4551401 commit f2f4a22

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/mapbox.android.ts

+27-13
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,8 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
389389
private onCameraIdleListener;
390390
private onLocationClickListener;
391391

392+
private iconFactory;
393+
392394
private _markers = [];
393395
private _polylines = [];
394396
private _polygons = [];
@@ -688,7 +690,7 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
688690
destroy(nativeMap?: any): Promise<void> {
689691
return new Promise(async (resolve, reject) => {
690692
this.clearEventListeners();
691-
693+
this.iconFactory = null;
692694
if (Trace.isEnabled()) {
693695
CLog(CLogTypes.info, 'destroy(): destroying mapbox view.');
694696
}
@@ -1267,13 +1269,19 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
12671269
if (marker.icon) {
12681270
// for markers from url see UrlMarker in https://github.com/mapbox/mapbox-gl-native/issues/5370
12691271
if (marker.icon.startsWith('res://')) {
1270-
const resourcename = marker.icon.substring(6);
1271-
const res = Utils.ad.getApplicationContext().getResources();
1272-
const identifier = res.getIdentifier(resourcename, 'drawable', Utils.ad.getApplication().getPackageName());
1273-
if (identifier === 0) {
1274-
console.log(`No icon found for this device density for icon ' ${marker.icon}'. Falling back to the default icon.`);
1272+
let cached = this.iconCache[marker.iconPath];
1273+
if (!cached) {
1274+
const resourcename = marker.icon.substring(6);
1275+
const res = Utils.ad.getApplicationContext().getResources();
1276+
const identifier = res.getIdentifier(resourcename, 'drawable', Utils.ad.getApplication().getPackageName());
1277+
if (identifier !== 0) {
1278+
cached = this.iconCache[marker.iconPath] = iconFactory.fromResource(identifier);
1279+
}
1280+
}
1281+
if (cached) {
1282+
markerOptions.setIcon(cached);
12751283
} else {
1276-
markerOptions.setIcon(iconFactory.fromResource(identifier));
1284+
console.log(`No icon found for this device density for icon ' ${marker.icon}'. Falling back to the default icon.`);
12771285
}
12781286
} else if (marker.icon.startsWith('http')) {
12791287
if (marker.iconDownloaded !== null) {
@@ -1285,13 +1293,19 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
12851293
}
12861294
}
12871295
} else if (marker.iconPath) {
1288-
const iconFullPath = path.join(knownFolders.currentApp().path, marker.iconPath.replace('~/', ''));
1289-
// if the file doesn't exist the app will crash, so checking it
1290-
if (File.exists(iconFullPath)) {
1291-
// could set width, height, retina, see https://github.com/Telerik-Verified-Plugins/Mapbox/pull/42/files?diff=unified&short_path=1c65267, but that's what the marker.icon param is for..
1292-
markerOptions.setIcon(iconFactory.fromPath(iconFullPath));
1296+
let cached = this.iconCache[marker.iconPath];
1297+
if (!cached) {
1298+
const iconFullPath = path.join(knownFolders.currentApp().path, marker.iconPath.replace('~/', ''));
1299+
// if the file doesn't exist the app will crash, so checking it
1300+
if (File.exists(iconFullPath)) {
1301+
// could set width, height, retina, see https://github.com/Telerik-Verified-Plugins/Mapbox/pull/42/files?diff=unified&short_path=1c65267, but that's what the marker.icon param is for..
1302+
cached = this.iconCache[marker.iconPath] = iconFactory.fromPath(iconFullPath);
1303+
}
1304+
}
1305+
if (cached) {
1306+
markerOptions.setIcon(cached);
12931307
} else {
1294-
console.log(`Marker icon not found, using the default instead. Requested full path: '" + ${iconFullPath}'.`);
1308+
console.log(`Marker icon not found, using the default instead. Requested path: '" + ${marker.iconPath}'.`);
12951309
}
12961310
}
12971311
marker.android = this._mapboxMapInstance.addMarker(markerOptions);

0 commit comments

Comments
 (0)