javascript - 通过 openlayers 渲染 opensensemap IDW 功能

标签 javascript openlayers openstreetmap

我正在尝试使用 openlayers 在 map 上渲染一些额外的功能。这些功能可以从 opensensemap API 获取,但由于某种原因它们没有呈现。由于我对 openlayers 完全陌生,而且对 javascript 也不太了解,所以我希望得到一些帮助。

实时代码:https://ttnkn.github.io/map/pax/

var GeoStyle = {
    'Point': new ol.style.Style({
        image: new ol.style.Icon({
            src: '../img/bike.png',
            scale: 0.075,
        })
    }),
    'Circle': new ol.style.Circle({
        fill: new ol.style.Fill({
            color: 'rgba(255,255,255,0.4)'
        }),
        stroke: ol.style.Stroke({
            color: '#3399CC',
            width: 1.25
        }),
        radius: 5
    })
};

function GeoStyleFunc (feature,resolution) {
    return GeoStyle[feature.getGeometry().getType()];
}

var VectorSource =  new ol.source.Vector({
    format: new ol.format.GeoJSON(),
    url: 'https://api.opensensemap.org/boxes?bbox=9.118815,47.653129,9.228427,47.698786&format=geojson&exposure=mobile',
});

var VectorTile = new ol.source.XYZ({
    url: 'http://tile.memomaps.de/tilegen/{z}/{x}/{y}.png ',
    attributions: 'Map &#169; <a href="https://www.openstreetmap.org">OSM</a> | Tiles &#169; <a href="memomaps.de">MeMoMaps</a> | Data &#169; <a href="https://opensensemap.org/">OpenSenseMap</a>'
});

var map = new ol.Map({
    target: document.getElementById('map'),
    layers: [
        new ol.layer.Tile({
            source: VectorTile
        }),
        new ol.layer.Vector({
            source: VectorSource,
            style: GeoStyleFunc
        })
    ],
    controls: ol.control.defaults({ zoom: true, attribution: true }),
    view: new ol.View({
        center: ol.proj.fromLonLat([9.173, 47.672]),
        zoom: 15,
        maxZoom: 17,
        minZoom: 13
    })
});

var url = 'https://api.opensensemap.org/statistics/idw?bbox=9.118815,47.653129,9.228427,47.698786&phenomenon=Temperatur&gridType=hex&cellWidth=2';
fetch(url).then(value => {
    value.json().then(value => {
        var featureJson = value.data.featureCollection;
        var features = (new ol.format.GeoJSON()).readFeatures(featureJson);

        var vectorSourceHEX = new ol.source.Vector({
            features: features,
            projection: ol.proj.get('EPSG:4326')
        });

        var vectorLayer = new ol.layer.Vector({
            source: vectorSourceHEX,
//            style: GeoStyleFunc
        });

        map.addLayer(vectorLayer);
    });
}, error => { console.log(error) });

最佳答案

矢量源中不使用投影选项。如果您使用 readFeatures,则必须将数据转换为 View 投影(当您使用自动完成的 url 构造源时)。

    var features = (new ol.format.GeoJSON()).readFeatures(featureJson, {
        dataProjection: 'EPSG:4326',
        featureProjection: map.getView().getProjection()
    });

    var vectorSource = new ol.source.Vector({
        features: features,
    });

关于javascript - 通过 openlayers 渲染 opensensemap IDW 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59443254/

相关文章:

javascript - 在不刷新 map 的情况下在 Openlayers 中动态设置矢量图层样式

geocoding - OpenStreetMap/Nominatim的地理编码问题

javascript - 如何在 TypeScript 中使用导航器和位置 DOM 变量

javascript - 在 img 大于 div 的不同 div 尺寸中垂直对齐不同的 img 尺寸

javascript - OpenLayers 纬度在 Webkit 移动浏览器中捕获不准确

reactjs - 如何强制一个 react 组件不重新渲染?

xml - 使用渗透将 POSTGIS 表转换为 .OSM

ios - MKMap 不显示 Open Street Map 瓦片覆盖

javascript - NativeScript-vue 应用程序中的 Laravel 身份验证,有人做过吗?

Javascript 动态输入掩码