1.地图事件(实现点击地图新增点,实现飞行视角--漫游以及点击复位)
效果如下(点击三下地依次出现三个点)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 1.导入ol依赖 -->
<link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
<script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
<style>
.btn {
position: fixed;
z-index: 100;
top: 30px;
right: 50px;
}
</style>
</head>
<body>
<!-- 2.设置地图的挂载点 -->
<div id="map">
</div>
<button class="btn">复位地图</button>
<script>
// 3.初始化一个高德图层
const gaode = new ol.layer.Tile({
title: "高德地图",
source: new ol.source.XYZ({
url: 'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
wrapX: false
})
});
// 4.初始化openlayer地图
const map = new ol.Map({
// 将初始化的地图设置到id为map的DOM元素上
target: "map",
// 设置图层
layers: [gaode],
view: new ol.View({
center: [114.30, 30.50],
// 设置地图放大级别
zoom: 14,
projection: "EPSG:4326"
})
})
// source-layer
var source = new ol.source.Vector({})
var layer = new ol.layer.Vector({
source
})
map.addLayer(layer);
// 给地图绑定一个点击事件
map.on("click", (evt) => {
var {
coordinate
} = evt;
var point = new ol.Feature({
geometry: new ol.geom.Point(coordinate)
})
source.addFeature(point);
// 实现飞行视角--漫游
const view = map.getView();
view.animate({
center: coordinate
})
})
// 复位按钮
var btn = document.querySelector(".btn");
btn.onclick = function() {
map.getView().animate({
center: [114.30, 30.50],
zoom: 6,
duration: 3000
})
}
</script>
</body>
</html>
2.加载本地geojson数据
新建一个data文件夹,给下面存放一个map.json的数据文件代码如下
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"geometry":{
"type":"Point",
"coordinates":[114.30,30.50]
}
}
]
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<!-- 1.导入ol依赖 -->
<link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
<script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
</head>
<body>
<!-- 2.设置地图的挂载点 -->
<div id="map">
</div>
<script>
// 3.初始化一个高德图层
const gaode = new ol.layer.Tile({
title: "高德地图",
source: new ol.source.XYZ({
url: 'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
wrapX: false
})
});
// 4.初始化openlayer地图
const map = new ol.Map({
// 将初始化的地图设置到id为map的DOM元素上
target: "map",
// 设置图层
layers: [gaode],
view: new ol.View({
center: [114.30, 30.50],
// 设置地图放大级别
zoom: 14,
projection: "EPSG:4326"
})
})
// 设置矢量数据源加载geojson数据
var source = new ol.source.Vector({
url: "./data/map.json",
format: new ol.format.GeoJSON()
})
//设置矢量图层
var layer = new ol.layer.Vector({
source
})
//设置点元素样式
let style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({
color: "red"
})
})
})
//将点样式加到图层上
layer.setStyle(style)
//将图层添加到地图上
map.addLayer(layer);
</script>
</body>
</html>
3.加载网络geojson数据
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<!-- 1.导入ol依赖 -->
<link rel="stylesheet" href="https://lib.baomitu.com/ol3/4.6.5/ol.css">
<script src="https://lib.baomitu.com/ol3/4.6.5/ol.js"></script>
</head>
<body>
<!-- 2.设置地图的挂载点 -->
<div id="map"></div>
<script type="text/javascript">
//3.初始化一个高德图层
const gaode = new ol.layer.Tile({
title: "高德地图",
source: new ol.source.XYZ({
url: 'https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',
wrapX: false
})
});
// 4.初始化openlayer地图
const map = new ol.Map({
// 将初始化的地图设置到id为map的DOM元素上
target: "map",
// 设置图层
layers: [gaode],
view: new ol.View({
center: [114.30, 30.50],
// 设置地图放大级别
zoom: 4,
projection: "EPSG:4326"
})
})
let china_source = new ol.source.Vector({
url: 'https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json',
format: new ol.format.GeoJSON(),
})
let china_style = new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(50,50,50,0.4)'
}),
stroke: new ol.style.Stroke({
color: "#ff2d5180",
width: 2
})
})
let china_layer = new ol.layer.Vector({
source: china_source
})
china_layer.setStyle(china_style);
map.addLayer(china_layer)
//添加多个区域颜色
// 陕西
const huibei_source = new ol.source.Vector({
url: "https://geo.datav.aliyun.com/areas_v3/bound/610000_full.json",
format: new ol.format.GeoJSON()
})
const huibei_layer = new ol.layer.Vector({
source: huibei_source
})
const huibei_style = new ol.style.Style({
fill: new ol.style.Fill({
color: '#333'
}),
})
huibei_layer.setStyle(huibei_style)
map.addLayer(huibei_layer)
</script>
</body>
</html>
其中https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json可以获取到国家地区区域信息你只需要改后面的区号即可获取到不同省份的区域
我用的是https://geo.datav.aliyun.com/areas_v3/bound/610000_full.json陕西区域
标签:map,layer,ol,基础,style,source,openlayers,new From: https://blog.csdn.net/qq_54878347/article/details/142926668