要素的查询、通过geserver进行的增删改 都是用到下图这个api,很重要!

image-20230618133355598

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
queryFeature(){ 
    //首先定义一个空的矢量图层,设置样式并添加到当前map中
    var vectorSource = new VectorSource();
    
    //设置查询参数与条件
    var featureRequest = new WFS().writeGetFeature({
    	srsName: 'EPSG:4326',//坐标系统
    	featureNS: 'http://onemap/ns',//命名空间 URI
    	featurePrefix: 'onemap',//工作区名称
    	featureTypes: [this.wfsSource],//查询图层,可以是同一个工作区下多个图层,逗号隔开
    	 maxFeatures: 5000,
    	outputFormat: 'application/json',
    	filter: new like('objectid','254*')//前者是属性名,后者是对应值 
    });
    
    fetch(api.domain + '/geoserver/' + '/onemap/ows?service=WFS', {//geoserver wfs地址如localhost:8080/geoserver/wfs,我是8081
    	method: 'POST',
    	body: new XMLSerializer().serializeToString(featureRequest)
    }).then(function(response) { 
    	return response.json();
    }).then(function(json) {  
    	console.log("feature json:",json);
    });
}

上述代码进行模糊查询一直报错??

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 at __uniappview.html:0

fetch的url写错了 , 请求的地址和请求wfs资源的一样就不会报错

1
api.domain + '/geoserver/onemap/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=' + this.wfsSource + '&outputFormat=application%2Fjson&srsname=EPSG:4326'

注意: like 只可以对字符串属性进行模糊查找, 整型不可以

image-20230618133505461