Skip to content

Commit 8aa323b

Browse files
committed
feat: getImage and removeImage
1 parent a5d5b29 commit 8aa323b

File tree

3 files changed

+117
-3
lines changed

3 files changed

+117
-3
lines changed

src/mapbox.android.ts

+49-2
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, path, ImageSource } from '@nativescript/core';
8+
import { AndroidApplication, Application, Color, File, Trace, Utils, knownFolders, path, ImageSource, Image } from '@nativescript/core';
99
import { getImage } from '@nativescript/core/http';
1010
import { FilterParser } from './filter/filter-parser.android';
1111
import { GeoUtils } from './geo.utils';
@@ -1100,6 +1100,31 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
11001100
});
11011101
}
11021102

1103+
async getImage(imageId: string, nativeMap?: any): Promise<ImageSource> {
1104+
return new Promise((resolve, reject) => {
1105+
const theMap = nativeMap || this._mapboxMapInstance;
1106+
1107+
if (!theMap) {
1108+
reject('No map has been loaded');
1109+
return;
1110+
}
1111+
1112+
try {
1113+
const nativeImage = theMap.getStyle().getImage(imageId);
1114+
const img = new ImageSource(nativeImage);
1115+
1116+
resolve(img);
1117+
} catch (ex) {
1118+
reject("Error during getImage: " + ex);
1119+
1120+
if (Trace.isEnabled()) {
1121+
CLog(CLogTypes.info, 'Error in mapbox.getImage: ' + ex);
1122+
}
1123+
throw ex;
1124+
}
1125+
});
1126+
}
1127+
11031128
async addImage(imageId: string, image: string, nativeMap?: any): Promise<void> {
11041129
return new Promise((resolve, reject) => {
11051130
const theMap = nativeMap || this._mapboxMapInstance;
@@ -1125,8 +1150,30 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
11251150
CLog(CLogTypes.info, 'Error in mapbox.addImage: ' + ex);
11261151
}
11271152
throw ex;
1128-
}
1153+
}
1154+
});
1155+
}
1156+
1157+
async removeImage(imageId: string, nativeMap?: any): Promise<void> {
1158+
return new Promise((resolve, reject) => {
1159+
const theMap = nativeMap || this._mapboxMapInstance;
11291160

1161+
if (!theMap) {
1162+
reject('No map has been loaded');
1163+
return;
1164+
}
1165+
1166+
try {
1167+
theMap.getStyle().removeImage(imageId);
1168+
resolve();
1169+
} catch (ex) {
1170+
reject("Error during removeImage: " + ex);
1171+
1172+
if (Trace.isEnabled()) {
1173+
CLog(CLogTypes.info, 'Error in mapbox.removeImage: ' + ex);
1174+
}
1175+
throw ex;
1176+
}
11301177
});
11311178
}
11321179

src/mapbox.common.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Color, ContentView, Property, Trace, booleanConverter } from '@nativescript/core';
1+
import { Color, ContentView, Property, Trace, booleanConverter, ImageSource } from '@nativescript/core';
22

33
export const MapboxTraceCategory = 'NativescriptMapbox';
44
export enum CLogTypes {
@@ -661,7 +661,11 @@ export interface MapboxApi {
661661

662662
getLayers(nativeMap?: any): Promise<LayerCommon[]>;
663663

664+
getImage(imageId: string, nativeMap?: any): Promise<ImageSource>
665+
664666
addImage(imageId: string, image: string, nativeMap?: any): Promise<void>;
667+
668+
removeImage(imageId: string, nativeMap?: any): Promise<void>;
665669
}
666670

667671
// ------------------------------------------------------------
@@ -806,6 +810,12 @@ export interface MapboxViewApi {
806810

807811
getLayers(nativeMap?: any): Promise<LayerCommon[]>;
808812

813+
getImage(imageId: string, nativeMap?: any): Promise<ImageSource>
814+
815+
addImage(imageId: string, image: string, nativeMap?: any): Promise<void>;
816+
817+
removeImage(imageId: string, nativeMap?: any): Promise<void>;
818+
809819
destroy(): Promise<any>;
810820

811821
onStart(): Promise<any>;
@@ -968,6 +978,15 @@ export abstract class MapboxViewCommonBase extends ContentView implements Mapbox
968978
getLayers(nativeMap?: any): Promise<LayerCommon[]> {
969979
return this.mapbox.getLayers(nativeMap);
970980
}
981+
getImage(imageId: string): Promise<ImageSource> {
982+
return this.mapbox.getImage(imageId, this.getNativeMapView());
983+
}
984+
addImage(imageId: string, image: string): Promise<void> {
985+
return this.mapbox.addImage(imageId, image, this.getNativeMapView());
986+
}
987+
removeImage(imageId: string): Promise<void> {
988+
return this.mapbox.removeImage(imageId, this.getNativeMapView());
989+
}
971990
destroy(): Promise<any> {
972991
return this.mapbox && this.mapbox.destroy(this.getNativeMapView());
973992
}

src/mapbox.ios.ts

+48
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,31 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
978978
});
979979
}
980980

981+
async getImage(imageId: string, nativeMap?: any): Promise<ImageSource> {
982+
return new Promise((resolve, reject) => {
983+
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
984+
985+
if (!theMap) {
986+
reject('No map has been loaded');
987+
return;
988+
}
989+
990+
try {
991+
const nativeImage = theMap.style.imageForName(imageId);
992+
const img = new ImageSource(nativeImage);
993+
994+
resolve(img);
995+
} catch (ex) {
996+
reject("Error during getImage: " + ex);
997+
998+
if (Trace.isEnabled()) {
999+
CLog(CLogTypes.info, 'Error in mapbox.getImage: ' + ex);
1000+
}
1001+
throw ex;
1002+
}
1003+
});
1004+
}
1005+
9811006
async addImage(imageId: string, image: string, nativeMap?: any): Promise<void> {
9821007
return new Promise((resolve, reject) => {
9831008
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
@@ -1008,6 +1033,29 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
10081033
});
10091034
}
10101035

1036+
async removeImage(imageId: string, nativeMap?: any): Promise<void> {
1037+
return new Promise((resolve, reject) => {
1038+
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
1039+
1040+
if (!theMap) {
1041+
reject('No map has been loaded');
1042+
return;
1043+
}
1044+
1045+
try {
1046+
theMap.style.removeImageForName(imageId);
1047+
resolve();
1048+
} catch (ex) {
1049+
reject('Error during removeImage: ' + ex);
1050+
1051+
if (Trace.isEnabled()) {
1052+
CLog(CLogTypes.info, 'Error in mapbox.removeImage: ' + ex);
1053+
}
1054+
throw ex;
1055+
}
1056+
});
1057+
}
1058+
10111059
addMarkers(markers: MapboxMarker[], nativeMap?: any): Promise<void> {
10121060
return new Promise((resolve, reject) => {
10131061
try {

0 commit comments

Comments
 (0)