基于vue+openlayer實(shí)現(xiàn)地圖聚合和撒點(diǎn)效果
前言:
openlayer是目前我們gis常用的一款開源的,并且反饋都特別好的軟件了,像之前的ol3, 風(fēng)靡一時(shí),地圖實(shí)現(xiàn)也很簡單,很實(shí)用,目前vue中使用地圖也是非常多的,那么如果在vue中引入openlayer并且實(shí)現(xiàn)地圖撒點(diǎn)效果,甚至是更深層的地圖聚合效果呢,本文來分享下vue中地圖的實(shí)現(xiàn)。目前openlayer的 5 系列,6.5 都是通用的,經(jīng)測試可用。
實(shí)現(xiàn)效果:
1、聚合效果:

2、撒點(diǎn)效果:

具體實(shí)現(xiàn)步驟:
1、項(xiàng)目中引入openlayer
cnpm i ol --save
2、配置(按需引入)
(1)新建一個(gè)vue文件
(2)template
<div id="map"></div>
(3)js部分
引入相關(guān)配置文件,這是我的所有引入,你可以根據(jù)你的情況刪一刪
import "ol/ol.css";
import View from "ol/View";
import Map from "ol/Map";
import TileLayer from "ol/layer/Tile";
import Overlay from "ol/Overlay";
import XYZ from "ol/source/XYZ";
import { Vector as SourceVec ,Cluster } from "ol/source";
import { Feature } from "ol";
import { Vector as LayerVec , Vector as VectorLayer } from "ol/layer";
import { Point, LineString } from "ol/geom";
import {
Style,
Icon,
Fill,
Stroke,
Text,
Circle as CircleStyle,
} from "ol/style";
import { OSM, TileArcGISRest } from "ol/source";
3、實(shí)現(xiàn)地圖展示


mounted:
mounted() {
this.initMap();
},
methods:我這里提供了兩種地圖的模板,都是在線的,內(nèi)網(wǎng)的話換成你自己的地址
initMap(){
//渲染地圖
var layers = [
//深藍(lán)色背景
new TileLayer({
source: new XYZ({
url:
"https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
}),
}),
//初始化背景
// new TileLayer({
// source: new OSM(),
// }),
];
this.map = new Map({
layers: layers,
target: "map",
view: new View({
projection: 'EPSG:4326',
center: [120, 30],
zoom: 10,
minZoom: 5,
maxZoom: 14
}),
});
//點(diǎn)擊提示當(dāng)前的坐標(biāo)
this.map.on(
"click",
function (evt) {
alert(evt.coordinate[0] + ";" + evt.coordinate[1]);
},
map
);
}
4、撒點(diǎn)功能

mounted:
mounted() {
this.initMap();
},
methods:
initMap(){
//渲染地圖
var layers = [
//深藍(lán)色背景
// new TileLayer({
// source: new XYZ({
// url:
// "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
// }),
// }),
//初始化背景
new TileLayer({
source: new OSM(),
}),
];
this.map = new Map({
layers: layers,
target: "map",
view: new View({
projection: 'EPSG:4326',
center: [120, 30],
zoom: 10,
minZoom: 5,
maxZoom: 14
}),
});
//點(diǎn)擊提示當(dāng)前的坐標(biāo)
this.map.on(
"click",
function (evt) {
alert(evt.coordinate[0] + ";" + evt.coordinate[1]);
},
map
);
//我這里是寫的固定數(shù)據(jù)點(diǎn),所以可以直接渲染完地址直接調(diào)用
this.addMarker()
},
addMarker(){
//創(chuàng)建畫板
let sourceArr = new SourceVec({});
//定義隨機(jī)數(shù)據(jù),這里隨機(jī)了200個(gè)
for (var i = 1; i <= 200; i++) {
//點(diǎn)的坐標(biāo)信息
let coordinates = [120.00 + Math.random(), 30.00 + Math.random()];
let feature = new Feature(new Point(coordinates));
let markerStyle = new Style({
image: new Icon({
opacity: 0.75,
src: this.fixedStationImg1,
}),
})
feature.setStyle(markerStyle)
sourceArr.addFeature(feature);
}
//LayerVec /VectorLayer 這兩種都可以
var layer = new VectorLayer({
source: sourceArr,
})
//地圖添加畫板
this.map.addLayer(
layer
);
}
5、聚合效果

mounted:
mounted() {
this.initMap();
},
methods:
initMap(){
//渲染地圖
var layers = [
//深藍(lán)色背景
// new TileLayer({
// source: new XYZ({
// url:
// "https://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}",
// }),
// }),
//初始化背景
new TileLayer({
source: new OSM(),
}),
];
this.map = new Map({
layers: layers,
target: "map",
view: new View({
projection: 'EPSG:4326',
center: [120, 30],
zoom: 10,
minZoom: 5,
maxZoom: 14
}),
});
//點(diǎn)擊提示當(dāng)前的坐標(biāo)
this.map.on(
"click",
function (evt) {
alert(evt.coordinate[0] + ";" + evt.coordinate[1]);
},
map
);
//我這里是寫的固定數(shù)據(jù)點(diǎn),所以可以直接渲染完地址直接調(diào)用
this.addMarker()
},
addMarker(){
//創(chuàng)建畫板
let sourceArr = new SourceVec({});
//定義隨機(jī)數(shù)據(jù),這里隨機(jī)了200個(gè)
for (var i = 1; i <= 200; i++) {
//點(diǎn)的坐標(biāo)信息
let coordinates = [120.00 + Math.random(), 30.00 + Math.random()];
let feature = new Feature(new Point(coordinates));
let markerStyle = new Style({
image: new Icon({
opacity: 0.75,
src: this.fixedStationImg1,
}),
})
feature.setStyle(markerStyle)
sourceArr.addFeature(feature);
}
//添加進(jìn)map層-聚合點(diǎn)-LayerVec /VectorLayer 這兩種都可以
var layer = new LayerVec({
source: this.ClusterSource,
style: function (feature, resolution) {
var size = feature.get('features').length;
//如果是聚合數(shù)為1也就是最底層的則是定位圖標(biāo)
if (size == 1) {
return new Style({
image: new Icon({
anchor: [0.5, 1],
src: require("../../assets/Img/marker_yes.png"),
})
})
}else {
//這里設(shè)置聚合部分的樣式
return new Style({
image: new CircleStyle({
radius: 30,
stroke: new Stroke({
color: 'white'
}),
fill: new Fill({
color: 'blue'
})
}),
text: new Text({
text: size.toString(),
fill: new Fill({
color: 'white'
})
})
})
}
}
})
//地圖添加畫板
this.map.addLayer(
layer
);
}
參考文獻(xiàn):
js中使用openlayer: https://blog.csdn.net/HerryDong/article/details/110951955
到此這篇關(guān)于vue+openlayer實(shí)現(xiàn)地圖聚合效果和撒點(diǎn)效果的文章就介紹到這了,更多相關(guān)vue openlayer地圖聚合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
three.js實(shí)現(xiàn)vr全景圖功能實(shí)例(vue)
去年全景圖在微博上很是火爆了一陣,正好我也做過一點(diǎn)全景相關(guān)的項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于three.js實(shí)現(xiàn)vr全景圖功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-05-05
vue3使用vue-cli引入windicss報(bào)錯(cuò)Can‘t resolve windi.css問題
這篇文章主要介紹了vue3使用vue-cli引入windicss報(bào)錯(cuò)Can‘t resolve windi.css問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue.js實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)的方法示例
這篇文章主要給大家介紹了vue.js實(shí)現(xiàn)請(qǐng)求數(shù)據(jù)的方法示例,文中分別介紹了vue1.0和vue2.0的示例方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02
vue中post請(qǐng)求以a=a&b=b 的格式寫遇到的問題
這篇文章主要介紹了vue中post請(qǐng)求以a=a&b=b 的格式寫遇到的問題,需要的朋友可以參考下2018-04-04
vue實(shí)現(xiàn)一個(gè)移動(dòng)端屏蔽滑動(dòng)的遮罩層實(shí)例
本篇文章主要介紹了vue實(shí)現(xiàn)一個(gè)移動(dòng)端屏蔽滑動(dòng)的遮罩層實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-06-06
vue+vue-fullpage實(shí)現(xiàn)整屏滾動(dòng)頁面的示例代碼(直播平臺(tái)源碼)
這篇文章主要介紹了vue+vue-fullpage實(shí)現(xiàn)整屏滾動(dòng)頁面,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06

