简体   繁体   English

如何在 openlayers 中更改 geosjon 图层样式?

[英]How to change geosjon layer style in openlayers?

I am new at OL.我是 OL 的新人。

I have a point geosjon file.我有一个点 geosjon 文件。

I want to change the colors of the point instead of the default color, and I can't figure out how to.我想更改点的颜色而不是默认颜色,但我不知道该怎么做。

I have tried to use Openlayers own guide for static styles - but it doesn't make much sense to me.我曾尝试使用 Openlayers 自己的静态样式指南 - 但这对我来说没有多大意义。 Link to the guide: https://openlayers.org/workshop/en/vector/style.html指南链接: https : //openlayers.org/workshop/en/vector/style.html

When I don't put in the code from the guidebook my map works, but when I do chrome says this: enter image description here当我没有输入指南中的代码时,我的地图可以工作,但是当我使用 chrome 时会说:在此处输入图像描述

I guess there is something wrong with my code, when I put the geosjon layer in the map - and I dont know what it is.我想我的代码有问题,当我将 geosjon 图层放在地图中时 - 我不知道它是什么。

So please help me.所以请帮助我。 The geosjon part of my .js code is:我的 .js 代码的 geosjon 部分是:

var trees_cologne = new ol.layer.Vector({
 title: 'Some trees of Cologne',
source: new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: 'trees.geojson'
}),
//import of the trees

const layer = trees({
source: 'trees.geojson',
style: new Style({
  fill: new Fill({
    color: 'red'
  }),
  stroke: new Stroke({
    color: 'white'
  })
})
});

For points the style is usually a circle which will need to have a radius defined.对于点,样式通常是一个需要定义半径的圆。 The fill and stroke should be defined inside that (the stroke width should be set smaller than the radius).填充和描边应该在里面定义(描边宽度应该设置为小于半径)。 Also check that your geojson is using the same coordinate system as your map and if they are different the projections must be specified as appropriate in the format: dataProjection in OL5 (but default DataProjection in OL4) for the geojson and featureProjection for the map view.还要检查您的 geojson 是否使用与地图相同的坐标系,如果它们不同,则必须以适当的格式指定投影: OL5 中的 dataProjection(但 OL4 中的默认DataProjection)用于 geojson 和 featureProjection 用于地图视图。 The complete layer setup should then look something like this:完整的图层设置应该如下所示:

var radius = 8;

var trees_cologne = new ol.layer.Vector({
    title: 'Some trees of Cologne',
    source: new ol.source.Vector({
        format: new ol.format.GeoJSON({ defaultDataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }),
        url: 'trees.geojson'
    }},
    style: new ol.style.Style({
        image: new ol.style.Circle({
            radius: radius,
            fill: new ol.style.Fill({
                color: 'red'
            }),
            stroke: new ol.style.Stroke({
                color: 'white',
                width: radius / 4
            })
        })
    })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM