完整代码
initMap() {
let map = new Map({
target: 'eguid-map',
controls: control.defaults({attribution: false, zoom: true, rotate: true}),
view: new View({
center: [119, 32],
maxZoom: 21,
zoom: 8,
minZoom: 7,
projection: 'EPSG:4326'
})
})
const xyzLayer = new TileLayer({
source: new XYZ({
url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile? lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
}),
zIndex: 1
})
return map
}
createLayer() {
let vectorSource = new VectorSource({
wrapX: false,
});
let vectorLayer = new VectorLayer({
source: vectorSource,
zIndex: 80,
style: getFeatureStyle;
return {
vectorSource,
vectorLayer,
}
}
addLayer(map) {
let vectorSource = new VectorSource({
wrapX: false,
});
let vectorLayer = new VectorLayer({
source: vectorSource,
zIndex: 80,
style: getFeatureStyle;
})
this.vectorSource = vectorSource
this.vectorLayer = vectorLayer
map.addLayer(this.vectorLayer);
}
getFeatureStyle(feature) {
const pixelRatio = DEVICE_PIXEL_RATIO;
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const pattern = (function () {
canvas.width = 8 * pixelRatio;
canvas.height = 8 * pixelRatio;
context.fillStyle = 'white';
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = 'rgba(102, 0, 102, 0.5)';
context.beginPath();
context.arc(4 * pixelRatio, 4 * pixelRatio, 3 * pixelRatio, 0, 2 * Math.PI);
context.fill();
context.fillStyle = 'rgb(55, 0, 170)';
context.beginPath();
context.arc(4 * pixelRatio, 4 * pixelRatio, 1.5 * pixelRatio, 0, 2 * Math.PI);
context.fill();
return context.createPattern(canvas, 'repeat');
})();
console.log('---pattern', pattern)
const circleStyle = new Style({
fill: new Fill({
color: pattern
}),
stroke: new Stroke({
color: '#333',
width: 2,
}),
})
return circleStyle
}
function createCircleFeature () {
let feature = new Feature({
geometry: new Circle(fromLonLat([124, 56]), 1000)
})
this.vectorSource.addFeature(feature )
}
var map = initMap()
map.addLayer()
createCircleFeature()