使用openlayers实现geojson数据的贴图效果
首先第一种要特别注意:url是一个地址 url 和 features 不要一起用
import { Map, View } from "ol";
import { Tile } from "ol/layer";
import { format } from "ol/coordinate";
import TileWMS from "ol/source/TileWMS";
import MousePosition from "ol/control/MousePosition";
import Vector from "ol/layer/Vector";
import {Style,Icon,Fill,Stroke,Text, Circle as CircleStyle} from "ol/style";
import { Feature } from "ol";
import VectorSource from "ol/source/Vector";
import { GeoJSON } from "ol/format";
addGeoJson() {
let color = '#3C3EBC';
if(this.disasterColour==='1'){
color= '#3C3EBC';
}else if(this.disasterColour==='2'){
color= '#FFFF00';
}else if(this.disasterColour==='3'){
color= '#FF8800 ';
}else if(this.disasterColour==='4'){
color= '#FF3333 ';
}
let styleFunction = function (feature) {
console.log( feature.values_.color, '颜色');
return new Style({
fill: new Fill({
color: color,
}),
});
};
let layers = this.map.getLayers();
console.log(layers, 'layers');
layers.array_.forEach(layer => {
if(layer.values_.title && layer.values_.title == 'dzm'){
this.map.removeLayer(layer);
}
});
let ly = new Vector({
title: "dzm",
source: new VectorSource({
projection: "EPSG:4326",
url: "static/json/aaa.json",
format: new GeoJSON(),
}),
style: styleFunction,
});
this.map.addLayer(ly);
},
第二种是不能使用这个方法 使用features
addGeoJson() {
let color = '#3C3EBC';
if(this.disasterColour==='1'){
color= '#3C3EBC';
}else if(this.disasterColour==='2'){
color= '#FFFF00';
}else if(this.disasterColour==='3'){
color= '#FF8800 ';
}else if(this.disasterColour==='4'){
color= '#FF3333 ';
}
let styleFunction = function (feature) {
console.log( feature.values_.color, '颜色');
return new Style({
fill: new Fill({
color: color,
}),
});
};
let layers = this.map.getLayers();
console.log(layers, 'layers');
layers.array_.forEach(layer => {
if(layer.values_.title && layer.values_.title == 'dzm'){
this.map.removeLayer(layer);
}
});
let arrGEOJSON=res,data.result
var features = new GeoJSON().readFeatures(arrGEOJSON)
let ly = new Vector({
title: "dzm",
source: new VectorSource({
projection: "EPSG:4326",
features:features
}),
style: styleFunction,
});
this.map.addLayer(ly);
}