VectorLayer 不与 OpenLayers 一起显示
VectorLayer not showing with OpenLayers
我基本上是在尝试重现 center example in my own JsFiddle
const geoJSON = {};
const features = new ol.format.GeoJSON().readFeatures(geoJSON);
const vectorSource = new ol.source.Vector({
features: features,
});
const style = new ol.style.Style({});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: style,
});
let olView = new ol.View({
center: ol.proj.fromLonLat([6.6339863, 46.5193823]),
zoom: 4,
});
let olLayers = [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
vectorLayer
];
new ol.Map({
layers: olLayers,
view: olView,
target: 'app',
});
我肯定遗漏了一些明显的东西,因为矢量图层没有显示在我的fiddle。
如果向下平移,您会看到瑞士的矢量位置错误
如果使用 readFeatures
,您需要指定要素投影,以便将 geojson Lon/Lat 坐标转换为视图投影
const features = new ol.format.GeoJSON().readFeatures(geoJSON, {featureProjection: olView.getProjection()});
我基本上是在尝试重现 center example in my own JsFiddle
const geoJSON = {};
const features = new ol.format.GeoJSON().readFeatures(geoJSON);
const vectorSource = new ol.source.Vector({
features: features,
});
const style = new ol.style.Style({});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: style,
});
let olView = new ol.View({
center: ol.proj.fromLonLat([6.6339863, 46.5193823]),
zoom: 4,
});
let olLayers = [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
vectorLayer
];
new ol.Map({
layers: olLayers,
view: olView,
target: 'app',
});
我肯定遗漏了一些明显的东西,因为矢量图层没有显示在我的fiddle。
如果向下平移,您会看到瑞士的矢量位置错误
如果使用 readFeatures
,您需要指定要素投影,以便将 geojson Lon/Lat 坐标转换为视图投影
const features = new ol.format.GeoJSON().readFeatures(geoJSON, {featureProjection: olView.getProjection()});