OpenLayers入门①(引入的是一个高德地图)

news/2025/1/11 4:01:26/

 OpenLayers入门(一) - 知乎

<!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>.ol-zoomslider {top: 7.5em;}</style>
</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"})})// 视图跳转控件const ZoomToExtent = new ol.control.ZoomToExtent({extent: [126.4, 45.7, 126.7, 45.9]})map.addControl(ZoomToExtent)// 放大缩小控件const zoomslider = new ol.control.ZoomSlider();map.addControl(zoomslider)// 全屏控件const fullScreen = new ol.control.FullScreen();map.addControl(fullScreen);// 1.通用样式信息和几何信息构建点要素// 几何const point = new ol.Feature({geometry: new ol.geom.Point([126.5350, 45.8021])});let style = new ol.style.Style({// image属性设置点要素的样式image: new ol.style.Circle({// radius设置点的半径 单位degreeradius: 10,fill: new ol.style.Fill({color: "#ff2d51"}),stroke:new ol.style.Stroke({width:2,color:"#333"})})})point.setStyle(style);// 2.将要素添加到矢量数据源let source = new ol.source.Vector({features: [point]})// 3.将矢量数据源添加到矢量图层let layer = new ol.layer.Vector({source})// 4.添加矢量图层到地图容器map.addLayer(layer)</script></body></html>

geojson

<!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>.ol-zoomslider {top: 7.5em;}</style>
</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"})})// 视图跳转控件const ZoomToExtent = new ol.control.ZoomToExtent({extent: [126.4, 45.7, 126.7, 45.9]})map.addControl(ZoomToExtent)// 放大缩小控件const zoomslider = new ol.control.ZoomSlider();map.addControl(zoomslider)// 全屏控件const fullScreen = new ol.control.FullScreen();map.addControl(fullScreen);// 创建geojson数据var data = {type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"Point",coordinates:[114.30,30.50]}}]}// 将数据添加到矢量数据源中var source = new ol.source.Vector({features:new ol.format.GeoJSON().readFeatures(data)})// 设置矢量图层var layer = new ol.layer.Vector({source})const style = new ol.style.Style({image:new ol.style.Circle({radius:10,fill:new ol.style.Fill({color:"#ff2d51"}),stroke:new ol.style.Stroke({color:"#333"})})})layer.setStyle(style)// 添加矢量图层到地图容器map.addLayer(layer)</script></body></html>

<!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"})})// 区域要素// 设置line要素的geojson数据var data = {type:"FeatureCollection",features:[{type:"Feature",geometry:{type:"LineString",coordinates:[[114.30,30.50],[116,30.31]]}},{type:"Feature",geometry:{type:"Polygon",coordinates:[[[114.30,30.50],[116,30.50],[116,30]]]}}]}// 设置矢量数据源读取数据let source = new ol.source.Vector({features:new ol.format.GeoJSON().readFeatures(data)})// 设置矢量图层let layer = new ol.layer.Vector({source})let style = new ol.style.Style({// stroke线设置样式stroke: new ol.style.Stroke({color:"#ff2d51",width:3}),fill:new ol.style.Fill({color:"rgba(50,50,50,0.3)"})})layer.setStyle(style)// 添加到地图容器map.addLayer(layer)</script></body></html>

加载本地的geojson数据

 

 

<!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.geojson",format:new ol.format.GeoJSON()})// 设置矢量图层var layer = new ol.layer.Vector({source})const style = new ol.style.Style({image:new ol.style.Circle({radius:8,fill:new ol.style.Fill({color:"#ff2d51"})})})layer.setStyle(style)// mapmap.addLayer(layer)</script></body></html>


<!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: 4,projection: "EPSG:4326"})})// 加载网络geojson数据const china_source = new ol.source.Vector({url:"https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json",format:new ol.format.GeoJSON()})const china_layer = new ol.layer.Vector({source:china_source})const 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})})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/420000_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>

地图事件及漫游

地图事件及漫游-CSDN直播

<!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-layervar source = new ol.source.Vector({})var layer = new ol.layer.Vector({source})map.addLayer(layer);// 给地图绑定一个点击事件map.on("click", (evt) => {var { coordinate } = evt;console.log(coordinate)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>


http://www.ppmy.cn/news/1451027.html

相关文章

云服务器+ASF实现全天挂卡挂时长

目录 前言正文1.安装下载2.编辑配置文件3.设置Steam社区证书4.启动ASF5.给游戏挂时长6.进阶-ASF自动启动且后台保活 前言 我遇到的最大的问题是&#xff0c;网络问题 其实不然&#xff0c;各大厂商的云服务器后台都有流量监控&#xff0c;意味着依靠一般方法是不能正常访问St…

Unity开发一个FPS游戏之四

在前面的系列中&#xff0c;我已介绍了如何实现一个基本的FPS游戏&#xff0c;这里将继续进行完善&#xff0c;主要是增加更换武器以及更多动作动画的功能。 之前我是采用了网上一个免费的3D模型来构建角色&#xff0c;这个模型自带了一把AR自动步枪&#xff0c;并且自带了一些…

ssm104园区停车管理系统+jsp

园区停车管理系统的设计与实现 摘 要 网络技术和计算机技术发展至今&#xff0c;已经拥有了深厚的理论基础&#xff0c;并在现实中进行了充分运用&#xff0c;尤其是基于计算机运行的软件更是受到各界的关注。加上现在人们已经步入信息时代&#xff0c;所以对于信息的宣传和管…

LangChain 入门7 格式化输出

概述&#xff1a; LangChain 提供的格式化输出功能具有多个优势&#xff0c;这些优势在处理和分析由 AI 生成的内容时尤其有用&#xff1a; 结构化数据&#xff1a;格式化输出允许 AI 的回应以结构化的方式呈现&#xff0c;如 JSON 对象&#xff0c;这使得数据更易于解析和处理…

golang面试题:怎么避免内存逃逸?

问题 怎么避免内存逃逸&#xff1f; 怎么答 在runtime/stubs.go:133有个函数叫noescape。noescape可以在逃逸分析中隐藏一个指针。让这个指针在逃逸分析中不会被检测为逃逸。 // noescape hides a pointer from escape analysis. noescape is// the identity function but …

MySql 前缀索引

MySQL中的前缀索引&#xff08;Prefix Index&#xff09;是一种特殊类型的索引&#xff0c;它仅对列值的前几个字符&#xff08;而不是整个列值&#xff09;建立索引。这种索引类型可以在一定程度上减少索引的大小&#xff0c;提高查询效率&#xff0c;特别是在处理长文本或字符…

Android 编译文件简述(Android.mk)

Android 编译文件简述(Android.mk) Android.mk 文件是 Android 构建系统中的一种构建配置文件,使用 GNU Make 语法,用于定义 Android 项目中的模块、库、应用程序、JNI 代码等的编译和链接方式。每个 Android.mk 文件通常对应一个目录,代表构建系统应该如何处理该目录下的源…

Kappa系数-评估分类算法的表现

#创作灵感# 涉及到算法的评价指标&#xff0c;其中有个 Kappa 系数&#xff0c;这里记录一下&#xff0c;便于理解。 #正文# 在机器学习领域&#xff0c;评价分类算法的准确性是至关重要的一环。而Kappa系数作为一种评价分类算法准确性的方法&#xff0c;能够帮助我们更全面地…