尝试在Openlayers 3上显示GeoJSON [英] Trying to display a GeoJSON on Openlayers 3

查看:126
本文介绍了尝试在Openlayers 3上显示GeoJSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在openlayers地图上显示geojson文件. openlayers地图已经可以使用,但是我无法弄清楚如何从geojson文件中显示要素.不幸的是,他们网站上的示例并不是很有用,因为它只是geojson对象直接写入文件中,然后再访问.我希望从一个单独的geojson文件中获取这些功能,并将其显示在地图上.

I am trying to display a geojson file on an openlayers map. The openlayers map is already working, however I cannot figure out how to display the features from the geojson file. The example on their website is unfortunately not very helpful, as it is simply the geojson object being written directly into the file and then accessed later. I wish to take the features from a separate geojson file and display them on the map.

这是我到目前为止从示例中直接复制的内容:

This is what I have so far, copied directly from the example:

var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

var vectorLayer = new ol.layer.Vector({
    source: vectorSource,
    style: styleFunction
});

var map = new ol.Map({
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        }),
        vectorLayer
    ],
    target: 'map',
    controls: ol.control.defaults({
        attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
        })
    }),
    view: new ol.View({
        center: [0, 0],
        zoom: 2
    })

});

我需要知道的是如何打开"文件并从geojson文件(当前位于url ..\public\geojson\federal_ridings.geojson中)获取功能,以代替已经存在的变量geojsonObject?

What I need to know is how do I "open" the file and get the features from the geojson file (currently located at the url ..\public\geojson\federal_ridings.geojson) in place of the variable geojsonObject which is already there?

推荐答案

要从外部文件添加GeoJSON层,请替换:

To add GeoJSON layer from external file replace:

var vectorSource = new ol.source.Vector({
    features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});

使用

var vectorSource = new ol.source.Vector({
    url: '..\public\geojson\federal_ridings.geojson',
    format: new ol.format.GeoJSON()
});

ol.format.GeoJSON文档

确保federal_ridings.geojson有效的JSON文件

演示

这篇关于尝试在Openlayers 3上显示GeoJSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆