问题描述
我的地图中有一个WFS图层.我在该图层中有建筑物,其中building_id作为属性以及其他许多属性.我的图层中也有坐标.
I have a WFS layer in my map. I have buildings in the layer with building_id as an attribute along with other many attributes. I have coordinates too in my layer.
我有一个网址: http://localhost:8080/geoserver/wfs?service = wfs& version = 1.0.0& request = getfeature& typename = topp:buildings& CQL_FILTER = id = 'bb21'
I have a URL: http://localhost:8080/geoserver/wfs?service=wfs&version=1.0.0&request=getfeature&typename=topp:buildings&CQL_FILTER=id='bb21'
如何使用此URL将地图缩放到该建筑物?
How can I use this URL to zoom my map to that building?
有什么想法吗?
AJ
================在这里更新==============
================updated here ===============
$.ajax({
url: 'http://localhost:8080/geoserver/wfs?service=wfs&version=1.0.0&request=getfeature&typename=genesis:Building_WGS&CQL_FILTER=HOUSE_NO=%271436%27',
xhrFields: {
withCredentials: true
},
success: function(data) {
var te = new ol.format.GML2();
var a = te.readFeatures(data)
var feature = new ol.Feature(a);
//console.log(test1);
var geom = feature.get("HOUSE_NO");
var view = map.getView();
//view.fit(geom, map.getSize());
console.log(geom);
}
});
推荐答案
首先,您必须使用ajax请求来查询url.许多JavaScript库都支持发出此类请求的简单方法,例如jQuery: http://api.jquery.com /jquery.ajax/
First, you'll have to query the url using an ajax request. Many JavaScript libraries support simple ways to make such request, such as jQuery: http://api.jquery.com/jquery.ajax/
然后,在收到请求响应后,您必须读取结果.如果您使用的是WFS,则您的响应应使用GML. OL3具有GML 2和3的格式:
Then, upon receiving the request response, you'll have to read the result. If you're using WFS, then your response should be in GML. OL3 has a format for GML 2 and 3:
- http://openlayers.org/en/v3. 9.0/apidoc/ol.format.GML2.html
- http://openlayers.org/en/v3. 9.0/apidoc/ol.format.GML3.html
- http://openlayers.org/en/v3.9.0/apidoc/ol.format.GML2.html
- http://openlayers.org/en/v3.9.0/apidoc/ol.format.GML3.html
都有一个readFeatures
方法,可用于将响应转换为一系列功能,即转换为ol.Feature
的实例.然后,您可以循环这些要素并获取其几何形状,并将地图拟合到该几何形状.
Both have a readFeatures
method you can use to transform your response into an array of features, i.e. into instances of ol.Feature
. Then you can loop in those features and get their geometry and fit the map to that geometry.
var geom = feature.getGeometry();
var view = map.getView();
view.fit(geom, map.getSize());
这篇关于单击URL时放大到矢量层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!