我在Openlayers 3.9.0中有一个矢量图层。 Geoserver从PostGIS获取数据,然后使用ol.source.Vector
获取该图层。返回数据的无法获取要素坐标,因此我可以获得最近的特征
GeoJSON格式导出为
{"type":"FeatureCollection","totalFeatures":422,"features":
[{"type":"Feature","id":"myLayer.709","geometry":{"type":"Point","coordinates":
[2391735.8907621,4695330.8039257005]},"geometry_name":"myLayer_geom","properties"
:{"myLayer_name":"Hello"....next feature
所以我通过ID得到的第一个特征,并尝试根据其坐标,找到其最接近的特征。这是我的代码
var bb = (sourceVector.getFeatureById('myLayer.709')).getCoordinates()
var aa = getClosestFeatureToCoordinate(bb);
console.log(aa);
我得到Cannot read property 'getCoordinates' of null
错误。
这是为什么?我错过了什么?
此外,getFeatureById
的API暗示该ID必须是一个数字,但在我的情况下是一个字符串(myLayer.709
)。另一方面,feature可以获得一个字符串作为一个ID和一个ID可以在数据读取过程中设置。
EDIT
这是整个代码(层切换模块here)
var textent = ol.proj.transformExtent([2297128.5, 4618333, 2459120.25, 4763120], 'EPSG:900913', 'EPSG:3857');
var layer = new ol.layer.Tile({
source: new ol.source.OSM({})});
var bingMapsAerial = new ol.layer.Tile({
source: new ol.source.BingMaps({
key: 'aaaa',
imagerySet:'AerialWithLabels'
})
});
var ait = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://localhost:8080/geoserver/mymap/wms?',
params: {'LAYERS': 'mymap:polygons, mymap:lines', 'TILED': true, 'VERSION': '1.3.0','FORMAT': 'image/png' ,'CRS': 'EPSG:900913'},
serverType: 'geoserver'
})
})
var sourceVector = new ol.source.Vector({
format: new ol.format.GeoJSON(),
useSpatialIndex : true,
loader: function (extent) {
$.ajax('http://localhost:5550/geoserver/mymap/wfs?service=WFS&version=1.0.0&request=GetFeature&typeName=mymap:mylayer&outputFormat=application/json&BBOX='+extent.join(','),
{type: 'GET'})
.done(
function(response) {
var geojsonFormat = new ol.format.GeoJSON({});
sourceVector.addFeatures(geojsonFormat.readFeatures(response,{dataProjection : projection,featureProjection : projection}));
})
.fail(function() {alert("no");});
},
strategy: ol.loadingstrategy.bbox
});
var fill = new ol.style.Fill({
color: 'rgba(0,0,0,0.2)'
});
var stroke = new ol.style.Stroke({
color: 'rgba(0,0,0,0.4)'
});
var circle = new ol.style.Circle({
radius: 6,
fill: fill,
stroke: stroke
});
layerVector = new ol.layer.Vector({
source: sourceVector,
style: new ol.style.Style({
fill: fill,
stroke: stroke,
image: circle
})
});
layer.set('title','a');
layer.set('type','base');
bingMapsAerial.set('title','b');
bingMapsAerial.set('type','base');
ait.set('title','ait');
ait.set('type','base');
var kbz = new ol.interaction.KeyboardZoom();
var dr = new ol.interaction.DragRotateAndZoom();
var control = new ol.control.FullScreen();
var ext = new ol.control.ZoomToExtent({extent: textent});
var center = ol.proj.transform([21.54967, 38.70250], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: center,
zoom: 6,
extent : textent,
maxZoom:20
});
var map = new ol.Map({
target: 'map',
layers:[bingMapsAerial, layer, ait, layerVector],
view: view
});
map.getView().fit(textent, map.getSize());
map.addInteraction(kbz);
map.addInteraction(dr);
map.addControl(control);
map.addControl(ext);
var layerSwitcher = new ol.control.LayerSwitcher();
map.addControl(layerSwitcher);
var bb = sourceVector.getFeatureById('mylayer.709').getGeometry().getCoordinates();
var aa = sourceVector.getClosestFeatureToCoordinate(bb);
console.log(aa);
UPDATE
在控制台中,如果我打map.getLayers()
我得到的矢量层和idIndex_
包含所有功能的阵列(709)。但在我的代码,如果我做var cc = sourceVector.getFeatures(); console.log(cc);
我得到[ ]
,只有两个空括号。这是否意味着在我尝试通过ID获取一个功能之前,功能尚未加载?
更新2 如果我删除
var bb=sourceVector.getFeatureById('mylayer.709').getGeometry().getCoordinates();
var aa = sourceVector.getClosestFeatureToCoordinate(bb);
console.log(aa);
,并把它放在读取功能的响应函数,WFS请求后,它的工作原理
.done(
function(response) {
var geojsonFormat = new ol.format.GeoJSON({});
sourceVector.addFeatures(geojsonFormat.readFeatures(response,{dataProjection :projection,featureProjection : projection}));
//-------------add the following
var bb=sourceVector.getFeatureById('mylayer.709').getGeometry().getCoordinates();
var aa = sourceVector.getClosestFeatureToCoordinate(bb);
console.log(aa);
})
这是否意味着该是我唯一的选择?为什么不能设置我的矢量图层,获取功能,设置我的地图和访问功能?在真实场景中,在完成所有加载操作后,用户会选择一个随机功能并获得更近的值。不可能知道身份证。在阅读时使用功能并不实际,也没有意义。有任何想法吗?
感谢
一个有用的观察。但是现在我得到'Uncaught TypeError:无法读取属性'getGeometry'的null'错误。似乎我无法正确地获取该功能,但我知道它在那里,我在地图上看到它,并且在我的'wfs'请求中没有出现错误。我看不到问题。有小费吗?谢谢 – slevin
查看更新的答案。 –
以我装载机我有'.done(\t \t \t 功能\t(响应){ VAR geojsonFormat =新ol.format.GeoJSON({}); sourceVector.addFeatures(geojsonFormat.readFeatures(响应,{dataProjection:投影,featureProjection:projection})); })'和'var projection = ol.proj.get('EPSG:3857');'。顺便说一下,我看到控制台中的所有图层,它们都被正确加载。 – slevin