Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,33 @@ Remove a layer added with addLayer() by id.
mapbox.removeLayer( id );
```

### queryRenderedFeatures
https://docs.mapbox.com/mapbox-gl-js/api/map/#map#queryrenderedfeatures
Returns an array of GeoJSON Feature objects representing visible features that satisfy the query parameters.

```js
mapbox
.queryRenderedFeatures({
point: {
lat: 52.3701494345567,
lng: 4.823684692382513,
},
layers: ['circle-with-source-object'],
filter: ['==', ['get', 'querySample'], '2'],
})
.then((result) => console.log('query rendered features', result))
```

### querySourceFeatures
https://docs.mapbox.com/mapbox-gl-js/api/map/#map#querysourcefeatures
Returns an array of GeoJSON Feature objects representing features within the specified vector tile or GeoJSON source that satisfy the query parameters.

```js
mapbox
.querySourceFeatures('source_id', { filter: ['==', ['get', 'querySample'], '2'] })
.then((result) => console.log('query source features', result));
```

### addLinePoint
Dynamically add a point to a line.

Expand Down
4 changes: 4 additions & 0 deletions demo/app/main-view-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,10 @@ export class HelloWorldModel extends Observable {
filter: ['all', ['==', '$id', '2']],
})
.then((result) => console.log('query rendered features', JSON.stringify(result)));

this.mapbox
.querySourceFeatures('circle-with-source-object_source', { filter: ['==', ['get', 'querySample'], '2'] })
.then((result) => console.log('query source features', JSON.stringify(result)));
}, 3000);
});

Expand Down
41 changes: 41 additions & 0 deletions src/mapbox.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
MapboxViewBase,
OfflineRegion,
QueryRenderedFeaturesOptions,
QuerySourceFeaturesOptions,
SetCenterOptions,
SetTiltOptions,
SetViewportOptions,
Expand Down Expand Up @@ -1538,6 +1539,46 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
});
}

querySourceFeatures(sourceId: string, options?: QuerySourceFeaturesOptions): Promise<Feature[]> {
return new Promise((resolve, reject) => {
try {
if (!options) {
options = {};
}

const source = this._mapboxMapInstance.getStyle().getSource(sourceId);
if (!source) {
throw new Error(`Source with id "${sourceId}" not found.`);
}

let features;
const queryFilter = options.filter ? FilterParser.parseJson(options.filter) : null;
if (source instanceof com.mapbox.mapboxsdk.style.sources.GeoJsonSource) {
features = source.querySourceFeatures(queryFilter);
} else if (source instanceof com.mapbox.mapboxsdk.style.sources.VectorSource) {
if (!options.sourceLayer) {
throw new Error('The option "sourceLayer" is required for vector sources.');
}
features = source.querySourceFeatures([options.sourceLayer], queryFilter);
} else {
throw new Error('Only sources from type "vector" or "geojson" are supported.');
}

const result = [];
for (let i = 0; i < features.size(); i++) {
const feature: com.mapbox.geojson.Feature = features.get(i);
result.push(JSON.parse(feature.toJson()));
}
resolve(result);
} catch (ex) {
if (Trace.isEnabled()) {
CLog(CLogTypes.info, 'Error in mapbox.querySourceFeatures: ' + ex);
}
reject(ex);
}
});
}

/**
*
* @deprecated
Expand Down
20 changes: 17 additions & 3 deletions src/mapbox.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ export interface QueryRenderedFeaturesOptions {

// ------------------------------------------------------------

export interface QuerySourceFeaturesOptions {
sourceLayer?: string;
filter?: any[];
}

// ------------------------------------------------------------

export interface Feature {
id?: any;
type: string;
Expand Down Expand Up @@ -615,6 +622,8 @@ export interface MapboxApi {

queryRenderedFeatures(options: QueryRenderedFeaturesOptions, nativeMap?: any): Promise<Feature[]>;

querySourceFeatures(sourceId: string, options?: QuerySourceFeaturesOptions, nativeMap?: any): Promise<Feature[]>;

addPolygon(options: AddPolygonOptions, nativeMap?: any): Promise<any>;

removePolygons(ids?: any[], nativeMap?: any): Promise<any>;
Expand Down Expand Up @@ -661,10 +670,10 @@ export interface MapboxApi {

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

getImage(imageId: string, nativeMap?: any): Promise<ImageSource>
getImage(imageId: string, nativeMap?: any): Promise<ImageSource>;

addImage(imageId: string, image: string, nativeMap?: any): Promise<void>;

removeImage(imageId: string, nativeMap?: any): Promise<void>;
}

Expand Down Expand Up @@ -739,7 +748,9 @@ export interface MapboxViewApi {
removeMarkers(options?: any): Promise<any>;

queryRenderedFeatures(options: QueryRenderedFeaturesOptions): Promise<Feature[]>;


querySourceFeatures(sourceId: string, options?: QuerySourceFeaturesOptions): Promise<Feature[]>;

setOnMapClickListener(listener: (data: LatLng) => boolean): Promise<any>;

setOnMapLongClickListener(listener: (data: LatLng) => boolean): Promise<any>;
Expand Down Expand Up @@ -957,6 +968,9 @@ export abstract class MapboxViewCommonBase extends ContentView implements Mapbox
queryRenderedFeatures(options: QueryRenderedFeaturesOptions): Promise<Feature[]> {
return this.mapbox.queryRenderedFeatures(options, this.getNativeMapView());
}
querySourceFeatures(sourceId: string, options?: QuerySourceFeaturesOptions): Promise<Feature[]> {
return this.mapbox.querySourceFeatures(sourceId, options, this.getNativeMapView());
}
addPolygon(options: AddPolygonOptions): Promise<any> {
return this.mapbox.addPolygon(options, this.getNativeMapView());
}
Expand Down
46 changes: 45 additions & 1 deletion src/mapbox.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DownloadOfflineRegionOptions,
Feature,
LatLng,
LayerCommon,
ListOfflineRegionsOptions,
MapStyle,
MapboxApi,
Expand All @@ -22,6 +23,7 @@ import {
MapboxViewBase,
OfflineRegion,
QueryRenderedFeaturesOptions,
QuerySourceFeaturesOptions,
SetCenterOptions,
SetTiltOptions,
SetViewportOptions,
Expand All @@ -32,7 +34,6 @@ import {
UserLocationCameraMode,
Viewport,
telemetryProperty,
LayerCommon,
} from './mapbox.common';

import { iOSNativeHelper } from '@nativescript/core/utils';
Expand Down Expand Up @@ -1422,6 +1423,49 @@ export class Mapbox extends MapboxCommon implements MapboxApi {
});
}

querySourceFeatures(sourceId: string, options?: QuerySourceFeaturesOptions, nativeMap?): Promise<Feature[]> {
return new Promise((resolve, reject) => {
try {
const theMap: MGLMapView = nativeMap || this._mapboxViewInstance;
if (!options) {
options = {};
}

const source = theMap.style.sourceWithIdentifier(sourceId);
if (!source) {
throw new Error(`Source with id "${sourceId}" not found.`);
}

let features;
const queryFilter = options.filter ? FilterParser.parseJson(options.filter) : null;
if (source instanceof MGLShapeSource) {
features = source.featuresMatchingPredicate(queryFilter);
} else if (source instanceof MGLVectorTileSource) {
if (!options.sourceLayer) {
throw new Error('The option "sourceLayer" is required for vector sources.');
}
const sourceLayerIds = options.sourceLayer ? NSSet.setWithArray<string>(iOSNativeHelper.collections.jsArrayToNSArray([options.sourceLayer])) : null;
features = source.featuresInSourceLayersWithIdentifiersPredicate(sourceLayerIds, queryFilter);
} else {
throw new Error('Only sources from type "vector" or "geojson" are supported.');
}

const result = [];
for (let i = 0; i < features.count; i++) {
const feature: MGLFeature = features.objectAtIndex(i);
const featureJson = NSJSONSerialization.dataWithJSONObjectOptionsError(feature.geoJSONDictionary(), 0);
result.push(JSON.parse(NSString.alloc().initWithDataEncoding(featureJson, NSUTF8StringEncoding) as any));
}
resolve(result);
} catch (ex) {
if (Trace.isEnabled()) {
CLog(CLogTypes.info, 'Error in mapbox.querySourceFeatures: ' + ex);
}
reject(ex);
}
});
}

/**
* @deprecated
*/
Expand Down