本文共 2603 字,大约阅读时间需要 8 分钟。
本文将详细介绍如何通过Web服务(WFS)技术从地图中搜索特定 osm_id 的实现方法。我们将使用 OpenLayers 库进行前端处理,并结合 Geoserver 作为后端服务,解决跨域访问问题。
// 构造一个WFS requestvar featureRequest = new ol.format.WFS().writeGetFeature({ srsName: 'EPSG:3857', featureNS: 'www.vnm.com', featurePrefix: 'VNM', featureTypes: ['gis.osm_waterways_free_1', 'gis.osm_buildings_a_free_1'], outputFormat: 'application/json', filter: ol.format.filter.like('osm_id', '*'+searchstr)}); fetch('http://gis.map.com/geoserver/VNM/wfs', { method: 'POST', body: new XMLSerializer().serializeToString(featureRequest)}).then(function(response) { return response.json();}).then(function(json) { var features = new ol.format.GeoJSON().readFeatures(json); if(features.length == 0) { alert("没有该项目"); return; } vectorSource.addFeatures(features); map.getView().fit(vectorSource.getExtent());}); 在实际应用中,可能会遇到跨域访问问题。为了解决这个问题,可以在前端使用 CORS 中间件,或者在后端配置 Geoserver 进行跨域设置。
var vectorSource = new ol.source.Vector();var selectVector = new ol.layer.Vector({ title:'选中区域', source: vectorSource, style: new ol.style.Style({ stroke: new ol.style.Stroke({ color: 'rgba(255, 255, 255, 1)', width: 2 }) })});var map;var vietnam;var strFromProj = "EPSG:4326";var strToProj = "EPSG:3857";// 初始化越南图层vietnam = new ol.layer.Tile({ title:'越南行政底图', type:'base', source: new ol.source.TileWMS({ url: "http://gis.map.com/geoserver/VNM/wms", params: { "LAYERS": "VNM:VNM" } })});map = new ol.Map({ target: "map", layers: [vietnam, selectVector], view: new ol.View({ center: ol.proj.transform([105.5, 15.5], strFromProj, strToProj), zoom: 5 })});// 添加图层切换控制器var layerSwitcher = new ol.control.LayerSwitcher({ tipLabel: 'Légende'});map.addControl(layerSwitcher); search() 函数进行数据查询。本文内容转载自 CNblogs 文章链接,转载请注明出处。