maptalks layers data query tool
npm i maptalks-gl
npm i maptalks.query
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/maptalks-gl/dist/maptalks-gl.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/maptalks.query/dist/maptalks.query.js"></script>
<!-- if need spatial query -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jsts.min.js"></script>
injectJSTS(jsts)Inject jsts namespace. If you need spatial query, this is necessary
import * as jsts from 'jsts';
import {
injectJSTS
} from 'maptalks.query';
injectJSTS(jsts);
Query classlayers query tool Class
constructor(map, options) new Query(map, options)
map: maptalks Mapoptions:config object
options.log: Whether to output logs when calculation errors occur const query = new Query(map, {
log: true
})
import {
Map,
TileLayer,
Polygon
} from 'maptalks-gl';
import {
Query,
injectJSTS
} from 'maptalk.query';
import * as jsts from 'jsts';
injectJSTS(jsts);
const mapView = {
'center': [120.54069005, 31.14989446],
'zoom': 9.49995108663881,
'pitch': 0,
'bearing': 0
};
// create map
const map = new Map('map1', {
...mapView,
baseLayer: new TileLayer('base', {
urlTemplate: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
subdomains: ['a', 'b', 'c', 'd'],
attribution: '© <a href="http://osm.org">OpenStreetMap</a> contributors, © <a href="https://carto.com/">CARTO</a>'
})
});
//create query instance
const query = new Query(map, {
log: true
});
query(options) simple data filter query. Keyword search, etc, return Promise
options.filter:data filter functionoptions.layers: The layer to be queried, if empty, will query all layers on the mapfunction getPropties(geo) {
let properties = geo.getPropties ? geo.getPropties() : geo.properties;
properties = properties || {};
if (properties.__original_properties) {
return properties.__original_properties;
}
return properties;
}
query.query({
filter: (geo, layer) => {
const properties = getPropties(geo);
const name = properties.name;
//mock simple keyword query
return name && name.includes('hello world');
},
layers: [layer, layer1, ...otherlayers]
}).then(result => {
console.log(result);
//do some things
}).catch(error => {
console.error(error);
});
spatialQuery(options) Geometric spatial query . return Promise
options.geometry: Input query geometry, maptalks. Polygon/maptalks. Circle/maptalks.
Rectangle…options.filter data filter function if needoptions.layers: The layer to be queried, if empty, will query all layers on the mapoptions.op Geometric Shape Relationship Query Operation, default value is ` Query.intersects`
op support list:
injectJSTS(jsts);
const polygon = new Polygon([...]);
query.spatialQuery({
geometry: polygon,
layers: [layer, layer1, ...otherlayers]
op: Query.intersects
}).then((result) => {
console.log(result);
//do some things
}).catch(error => {
console.error(error);
});
dispose()query.dispose();