openlayers3 实现测距 面积
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
- <title></title>
- <link href="~/Scripts/OpenLayers-demo/css/ol.css" rel="stylesheet" />
- <script src="~/Scripts/OpenLayers-demo/build/ol.js"></script>
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script src="~/Scripts/map/mapzoomtool.js" charset="gb2312"></script>
- <link href="~/Content/mapCtrlsstyles.css" rel="stylesheet" />
- <link href="~/Content/resources/KitchenSink-all.css" rel="stylesheet" />
- <script src="~/Scripts/ext-all.js"></script>
- <style type="text/css">
- #map {
- width: 100%;
- height: 100%;
- position: absolute;
- }
- #menu {
- float: left;
- position: absolute;
- bottom: 10px;
- left: 10px;
- z-index: 2000;
- }
- .checkbox {
- left: 20px;
- }
- /**
- * 提示框的样式信息
- */
- .tooltip {
- position: relative;
- background: rgba(0, 0, 0, 0.5);
- border-radius: 4px;
- color: white;
- padding: 4px 8px;
- opacity: 0.7;
- white-space: nowrap;
- }
- .tooltip-measure {
- opacity: 1;
- font-weight: bold;
- }
- .tooltip-static {
- background-color: #ffffff;
- color: black;
- border: 1px solid white;
- }
- .tooltip-measure:before,
- .tooltip-static:before {
- border-top: 6px solid rgba(0, 0, 0, 0.5);
- border-right: 6px solid transparent;
- border-left: 6px solid transparent;
- content: "";
- position: absolute;
- bottom: -6px;
- margin-left: -7px;
- left: 50%;
- }
- .tooltip-static:before {
- border-top-color: #ffffff;
- }
- #scalebar {
- float: left;
- margin-bottom: 10px;
- }
- </style>
- <script type="text/javascript">
- $(function () {
- //初始化地图
- var map = new ol.Map({
- target: 'map',
- layers: [
- new ol.layer.Tile({
- source:new ol.source.OSM()
- })
- ],
- view: new ol.View({
- center: new ol.proj.fromLonLat([114.4250, 23.0890]),
- zoom: 18,
- maxZoom: 20
- })
- });
- //定义矢量数据源
- var source = new ol.source.Vector();
- //定义矢量图层
- var vector = new ol.layer.Vector({
- source: source,
- style: new ol.style.Style({
- fill: new ol.style.Fill({
- color:'rgba(255,255,255,0.2)'
- }),
- stroke: new ol.style.Stroke({
- color: '#e21e0a',
- width:2
- }),
- image: new ol.style.Circle({
- radius: 5,
- fill: new ol.style.Fill({
- color:'#ffcc33'
- })
- })
- })
- });
- //将矢量图层添加到地图中
- map.addLayer(vector);
- //添加比例尺控件
- var scaleLineControl = new ol.control.ScaleLine({
- units: 'metric',
- target: 'scalebar',
- className: 'ol-scale-line'
- });
- map.addControl(scaleLineControl);
- //创建一个WGS84球体对象
- var wgs84Sphere = new ol.Sphere(6378137);
- //创建一个当前要绘制的对象
- var sketch = new ol.Feature();
- //创建一个帮助提示框对象
- var helpTooltipElement;
- //创建一个帮助提示信息对象
- var helpTooltip;
- //创建一个测量提示框对象
- var measureTooltipElement;
- //创建一个测量提示信息对象
- var measureTooltip;
- //继续绘制多边形的提示信息
- var continuePolygonMsg = 'Click to continue drawing the polygon';
- //继续绘制线段的提示信息
- var continueLineMsg = 'Click to continue drawing the line';
- //鼠标移动触发的函数
- var pointerMoveHandler = function (evt) {
- //Indicates if the map is currently being dragged.
- //Only set for POINTERDRAG and POINTERMOVE events. Default is false.
- //如果是平移地图则直接结束
- if (evt.dragging) {
- return;
- }
- //帮助提示信息
- var helpMsg = 'Click to start drawing';
- if (sketch) {
- //Get the feature's default geometry.
- //A feature may have any number of named geometries.
- //获取绘图对象的几何要素
- var geom = sketch.getGeometry();
- //如果当前绘制的几何要素是多边形,则将绘制提示信息设置为多边形绘制提示信息
- //如果当前绘制的几何要素是多线段,则将绘制提示信息设置为多线段绘制提示信息
- if (geom instanceof ol.geom.Polygon) {
- helpMsg = continuePolygonMsg;
- } else if (geom instanceof ol.geom.LineString) {
- helpMsg = continueLineMsg;
- }
- }
- //设置帮助提示要素的内标签为帮助提示信息
- helpTooltipElement.innerHTML = helpMsg;
- //设置帮助提示信息的位置
- //The coordinate in view projection corresponding to the original browser event.
- helpTooltip.setPosition(evt.coordinate);
- //移除帮助提示要素的隐藏样式
- $(helpTooltipElement).removeClass('hidden');
- };
- //触发pointermove事件
- map.on('pointermove', pointerMoveHandler);
- //当鼠标移除地图视图的时为帮助提示要素添加隐藏样式
- $(map.getViewport()).on('mouseout', function () {
- $(helpTooltipElement).addClass('hidden');
- });
- //获取大地测量复选框
- var geodesicCheckbox = document.getElementById('geodesic');
- //获取类型
- var typeSelect = document.getElementById('type');
- //定义一个交互式绘图对象
- var draw;
- //添加交互式绘图对象的函数
- function addInteraction() {
- // 获取当前选择的绘制类型
- var type = typeSelect.value == 'area' ? 'Polygon' : 'LineString';
- //创建一个交互式绘图对象
- draw = new ol.interaction.Draw({
- //绘制的数据源
- source: source,
- //绘制类型
- type: type,
- //样式
- style: new ol.style.Style({
- fill: new ol.style.Fill({
- color:'rgba(255,255,255,0.2)'
- }),
- stroke: new ol.style.Stroke({
- color: 'rgba(0,0,0,0.5)',
- lineDash: [10, 10],
- width:2
- }),
- image: new ol.style.Circle({
- radius: 5,
- stroke: new ol.style.Stroke({
- color:'rgba(0,0,0,0.7)'
- }),
- fill: new ol.style.Fill({
- color: 'rgba(255,255,255,0.2)'
- })
- })
- })
- });
- //将交互绘图对象添加到地图中
- map.addInteraction(draw);
- //创建测量提示框
- createMeasureTooltip();
- //创建帮助提示框
- createHelpTooltip();
- //定义一个事件监听
- var listener;
- //定义一个控制鼠标点击次数的变量
- var count = 0;
- //绘制开始事件
- draw.on('drawstart', function (evt) {
- //The feature being drawn.
- sketch = evt.feature;
- //提示框的坐标
- var tooltipCoord = evt.coordinate;
- //监听几何要素的change事件
- //Increases the revision counter and dispatches a 'change' event.
- listener = sketch.getGeometry().on('change', function (evt) {
- //The event target.
- //获取绘制的几何对象
- var geom = evt.target;
- //定义一个输出对象,用于记录面积和长度
- var output;
- if (geom instanceof ol.geom.Polygon) {
- map.removeEventListener('singleclick');
- map.removeEventListener('dblclick');
- //输出多边形的面积
- output = formatArea(geom);
- //Return an interior point of the polygon.
- //获取多变形内部点的坐标
- tooltipCoord = geom.getInteriorPoint().getCoordinates();
- } else if (geom instanceof ol.geom.LineString) {
- //输出多线段的长度
- output = formatLength(geom);
- //Return the last coordinate of the geometry.
- //获取多线段的最后一个点的坐标
- tooltipCoord = geom.getLastCoordinate();
- }
- //设置测量提示框的内标签为最终输出结果
- measureTooltipElement.innerHTML = output;
- //设置测量提示信息的位置坐标
- measureTooltip.setPosition(tooltipCoord);
- });
- //地图单击事件
- map.on('singleclick', function (evt) {
- //设置测量提示信息的位置坐标,用来确定鼠标点击后测量提示框的位置
- measureTooltip.setPosition(evt.coordinate);
- //如果是第一次点击,则设置测量提示框的文本内容为起点
- if (count == 0) {
- measureTooltipElement.innerHTML = "起点";
- }
- //根据鼠标点击位置生成一个点
- var point = new ol.geom.Point(evt.coordinate);
- //将该点要素添加到矢量数据源中
- source.addFeature(new ol.Feature(point));
- //更改测量提示框的样式,使测量提示框可见
- measureTooltipElement.className = 'tooltip tooltip-static';
- //创建测量提示框
- createMeasureTooltip();
- //点击次数增加
- count++;
- });
- //地图双击事件
- map.on('dblclick', function (evt) {
- //根据
- var point = new ol.geom.Point(evt.coordinate);
- source.addFeature(new ol.Feature(point));
- });
- }, this);
- //绘制结束事件
- draw.on('drawend', function (evt) {
- count = 0;
- //设置测量提示框的样式
- measureTooltipElement.className = 'tooltip tooltip-static';
- //Set the offset for this overlay.
- //设置偏移量
- measureTooltip.setOffset([0, -7]);
- //清空绘制要素
- sketch = null;
- //清空测量提示要素
- measureTooltipElement = null;
- //创建测量提示框
- createMeasureTooltip();
- //Removes an event listener using the key returned by on() or once().
- //移除事件监听
- ol.Observable.unByKey(listener);
- //移除地图单击事件
- map.removeEventListener('singleclick');
- }, this);
- }
- //创建帮助提示框
- function createHelpTooltip() {
- //如果已经存在帮助提示框则移除
- if (helpTooltipElement) {
- helpTooltipElement.parentNode.removeChild(helpTooltipElement);
- }
- //创建帮助提示要素的div
- helpTooltipElement = document.createElement('div');
- //设置帮助提示要素的样式
- helpTooltipElement.className = 'tooltip hidden';
- //创建一个帮助提示的覆盖标注
- helpTooltip = new ol.Overlay({
- element: helpTooltipElement,
- offset: [15, 0],
- positioning:'center-left'
- });
- //将帮助提示的覆盖标注添加到地图中
- map.addOverlay(helpTooltip);
- }
- //创建测量提示框
- function createMeasureTooltip() {
- //创建测量提示框的div
- measureTooltipElement = document.createElement('div');
- measureTooltipElement.setAttribute('id','lengthLabel');
- //设置测量提示要素的样式
- measureTooltipElement.className = 'tooltip tooltip-measure';
- //创建一个测量提示的覆盖标注
- measureTooltip = new ol.Overlay({
- element: measureTooltipElement,
- offset: [0, -15],
- positioning:'bottom-center'
- });
- //将测量提示的覆盖标注添加到地图中
- map.addOverlay(measureTooltip);
- }
- //测量类型发生改变时触发事件
- typeSelect.onchange = function () {
- //移除之前的绘制对象
- map.removeInteraction(draw);
- //重新进行绘制
- addInteraction();
- };
- //格式化测量长度
- var formatLength = function (line) {
- //定义长度变量
- var length;
- //如果大地测量复选框被勾选,则计算球面距离
- if (geodesicCheckbox.checked) {
- //Return the coordinates of the linestring.
- //获取坐标串
- var coordinates = line.getCoordinates();
- //初始长度为0
- length = 0;
- //获取源数据的坐标系
- var sourceProj = map.getView().getProjection();
- //进行点的坐标转换
- for (var i = 0; i < coordinates.length - 1; i++) {
- //第一个点
- var c1 = ol.proj.transform(coordinates[i], sourceProj, 'EPSG:4326');
- //第二个点
- var c2 = ol.proj.transform(coordinates[i + 1], sourceProj, 'EPSG:4326');
- //获取转换后的球面距离
- //Returns the distance from c1 to c2 using the haversine formula.
- length += wgs84Sphere.haversineDistance(c1,c2);
- }
- } else {
- //Return the length of the linestring on projected plane.
- //计算平面距离
- length = Math.round(line.getLength() * 100) / 100;
- }
- //定义输出变量
- var output;
- //如果长度大于1000,则使用km单位,否则使用m单位
- if (length > 1000) {
- output = (Math.round(length / 1000 * 100) / 100) + ' ' + 'km'; //换算成KM单位
- } else {
- output = (Math.round(length * 100) / 100) + ' ' + 'm'; //m为单位
- }
- return output;
- };
- //格式化测量面积
- var formatArea = function (polygon) {
- //定义面积变量
- var area;
- //如果大地测量复选框被勾选,则计算球面面积
- if (geodesicCheckbox.checked) {
- //获取初始坐标系
- var sourceProj = map.getView().getProjection();
- //Make a complete copy of the geometry.
- //Transform each coordinate of the geometry from one coordinate reference system to another.
- //The geometry is modified in place. For example, a line will be transformed to a line and a circle to a circle.
- //If you do not want the geometry modified in place, first clone() it and then use this function on the clone.
- //克隆该几何对象然后转换坐标系
- var geom = polygon.clone().transform(sourceProj, 'EPSG:4326');
- //Return the Nth linear ring of the polygon geometry.
- //Return null if the given index is out of range.
- //The exterior linear ring is available at index 0 and the interior rings at index 1 and beyond.
- //获取多边形的坐标系
- var coordinates = geom.getLinearRing(0).getCoordinates();
- //Returns the geodesic area for a list of coordinates.
- //获取球面面积
- area = Math.abs(wgs84Sphere.geodesicArea(coordinates));
- } else {
- //获取平面面积
- area = polygon.getArea();
- }
- //定义输出变量
- var output;
- //当面积大于10000时,转换为平方千米,否则为平方米
- if (area > 10000) {
- output = (Math.round(area/1000000*100)/100) + ' ' + 'km<sup>2</sup>';
- } else {
- output = (Math.round(area*100)/100) + ' ' + 'm<sup>2</sup>';
- }
- return output;
- };
- //添加交互绘图对象
- addInteraction();
- });
- </script>
- </head>
- <body>
- <div id="map">
- <div id="menu">
- <label>测量类型选择</label>
- <select id="type">
- <option value="length">长度</option>
- <option value="area">面积</option>
- </select>
- <label class="checkbox"><input type="checkbox" id="geodesic" />使用大地测量</label>
- </div>
- </div>
- <div id="scalebar"></div>
- </body>
- </html>
openlayers3 实现测距 面积的更多相关文章
- 百度地图整合功能分享修正版[ZMap.js] 实例源码!
ZMap 功能说明 ZMap 是学习百度地图 api 接口,开发基本功能后整的一个脚本类,本类方法功能大多使用 prototype 原型 实现: 包含的功能有:轨迹回放,圈画区域可编辑,判断几个坐标是 ...
- 百度地图API开发一——仿照现有测距效果实现测面功能
最近做了一个百度地图API的开发项目,里面有测量距离和测量面积的功能需求.测量距离百度给出了封装好的javascript包——BMapLib.DistanceTool,效果如下: 这个效果体验很好 ...
- 利用Python测量滴水湖的水面面积
美丽的滴水湖 美丽的滴水湖坐落在上海的东南角,濒临东海,风景秀丽,安静舒适,是旅游.恋爱的绝佳去处.笔者有幸去过一回,对那儿的风土人情留下了深刻的印象,如果有机会,笔者还会多去几次! 滴水湖是 ...
- 玩转X-CTR100 l STM32F4 l HC-SR04超声波测距
我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 超声波测距模块HC-SR04可以测量2cm~40 ...
- 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内。以及两点间的测距功能
权声明:本文为博主原创文章,未经博主允许不得转载. 利用百度API(JavaScript 版)实现在地图上绘制任一多边形,并判断给定经纬度是否在多边形范围内.以及两点间的测距功能. 绘制多边形(蓝色) ...
- ArcGIS Runtime SDK for WPF之测量距离和面积
bu不多说,上代码 using System.Windows; using ESRI.ArcGIS.Client; using ESRI.ArcGIS.Client.Tasks; using ESRI ...
- openlayers之地图测距侧面
项目背景vue-cli3.0 public下html需要引入文件 <link rel="stylesheet" href="<%= BASE_URL %> ...
- Openlayers3 WebGis二次开发包实例
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs& ...
- C语言单片机项目实战超声波雷达测距
本实验是基于MSP430利用HC-SR04超声波传感器进行测距,测距范围是3-65cm,讲得到的数据显示在LCD 1602液晶屏上. 模块工作原理如下 (1)采用 IO 触发测距,给至少 10us 的 ...
随机推荐
- springboot动态修改日志级别+权限认证
1. springboot动态修改日志级别+权限认证 1.1. 需求 网上找到的动态修改日志级别的方式,基本都是没有权限验证的,或者特地关闭权限验证,但也没给出加上验证的解决方式 修改日志等级也是一个 ...
- base64编解码学习及python代码实现
Base64是一种用64个字符来表示任意二进制数据的方法. Base64编码可以成为密码学的基石.可以将任意的二进制数据进行Base64编码.所有的数据都能被编码为并只用65个字符就能表示的文本文件. ...
- Group By Grouping Sets
Group by分组函数的自定义,与group by配合使用可更加灵活的对结果集进行分组,Grouping sets会对各个层级进行汇总,然后将各个层级的汇总值union all在一起,但却比单纯的g ...
- PHP的会话控制
页面数据的作用域: 当前页共享数据:变量.常量 两个页面间传递数据:get,post 跟踪用户的多页面数据共享(会话):session.cookie 全局数据共享:文件.数据库.memcached C ...
- Python 游戏之旅(Pygame)
Pygame是跨平台Python模块,专为电子游戏设计,包含图像.声音.建立在SDL基础上,允许实时电子游戏研发而无需被低级语言(如机器语言和汇编语言)束缚.基于这样一个设想,所有需要的游戏功能和理念 ...
- pycharm安装plugins
1 . 启动 PyCharm 软件 2 . 打开 设置(Setting) 窗口.(操作:File -> Setting 或者 按快捷键 Ctrl + Alt + S) 3 . 在搜索框里面搜索: ...
- Chapter 4 Invitations——10
"Mr. Cullen?" the teacher called, seeking the answer to a question that I hadn't heard. “C ...
- TOMCAT启动流程分析
------------------tomcat服务开启----------2014-9-26 9:17:07 org.apache.catalina.core.AprLifecycleListene ...
- 02 Tensorflow Hello World程序
打开python idle,输入: import tensorflow as tf # 创建常量 hello = tf.constant('Hello,world!') # 创建会话 sess = t ...
- 【原创】驱动开发中Memory read error导致的蓝屏问题
最近在看着<windows驱动开发技术详解>这本书,模仿着敲了第七章中的模拟文件读写部分.在Debug过程中,蓝屏了好多次并出现了各种奇葩的问题.在调了快两天之后,问题终于解决了!现在在这 ...