OpenLayers3基础教程——OL3之Popup

摘要:
概述:本节重点介绍在OpenLayers3中调用Popup时的实现。OL3使用Overlay而不是OL2的Popup函数。

概述:

本节重点讲述OpenLayers3中Popup的调用时实现,OL3改用Overlay取代OL2的Popup功能。


接口简单介绍:

overlay跟ol.control.Control一样,是一个可见的窗体,可是不和Control一样,不是固定在地图区域的某个部分,而是显示在一个地图坐标上,随着地图的移动或者缩放而移动的。其调用方式例如以下:

var popup = new ol.Overlay({
  element: document.getElementById('popup')
});
popup.setPosition(coordinate);
map.addOverlay(popup);

new ol.Overlay(options)

NameTypeDescription
options

Overlay options.

NameTypeDescription
elementElement | undefined

The overlay element.

offsetArray.<number> |undefined

Offsets in pixels used when positioning the overlay. The fist element in the array is the horizontal offset. A positive value shifts the overlay right. The second element in the array is the vertical offset. A positive value shifts the overlay down. Default is [0, 0].

positionol.Coordinate |undefined

The overlay position in map projection.

positioningol.OverlayPositioningstring | undefined

Defines how the overlay is actually positioned with respect to its position property. Possible values are'bottom-left''bottom-center''bottom-right''center-left''center-center','center-right''top-left''top-center', and 'top-right'. Default is 'top-left'.

stopEventboolean | undefined

Whether event propagation to the map viewport should be stopped. Default is true. If true the overlay is placed in the same container as that of the controls (CSS class name ol-overlaycontainer-stopevent); iffalse it is placed in the container with CSS class name ol-overlaycontainer.

insertFirstboolean | undefined

Whether the overlay is inserted first in the overlay container, or appended. Default is true. If the overlay is placed in the same container as that of the controls (see the stopEvent option) you will probably setinsertFirst to true so the overlay is displayed below the controls.

Fires:

Extends

Observable Properties

NameTypeSettableol.ObjectEvent typeDescription
elementElement | undefinedyeschange:element

The Element containing the overlay.

mapol.Map | undefinedyeschange:map

The map that the overlay is part of.

offsetArray.<number>yeschange:offset

The offset.

positionol.Coordinate | undefinedyeschange:position

The spatial point that the overlay is anchored at.

positioningol.OverlayPositioningyeschange:positioning

How the overlay is positioned relative to its point on the map.

Methods

getElement(){Element|undefined}

Get the DOM element of this overlay.

Returns:
The Element containing the overlay. 

getMap(){ol.Map|undefined}

Get the map associated with this overlay.

Returns:
The map that the overlay is part of. 

getOffset(){Array.<number>}

Get the offset of this overlay.

Returns:
The offset. 

getPosition(){ol.Coordinate|undefined}

Get the current position of this overlay.

Returns:
The spatial point that the overlay is anchored at. 

Get the current positioning of this overlay.

Returns:
How the overlay is positioned relative to its point on the map. 

on(type, listener, opt_this){goog.events.Key} inherited

Listen for a certain type of event.

NameTypeDescription
typestring | Array.<string>

The event type or array of event types.

listenerfunction

The listener function.

thisObject

The object to use as this in listener.

Returns:
Unique key for the listener. 

once(type, listener, opt_this){goog.events.Key} inherited

Listen once for a certain type of event.

NameTypeDescription
typestring | Array.<string>

The event type or array of event types.

listenerfunction

The listener function.

thisObject

The object to use as this in listener.

Returns:
Unique key for the listener. 

setElement(element)

Set the DOM element to be associated with this overlay.

NameTypeDescription
elementElement | undefined

The Element containing the overlay.

Set the map to be associated with this overlay.

NameTypeDescription
mapol.Map | undefined

The map that the overlay is part of.

setOffset(offset)

Set the offset for this overlay.

NameTypeDescription
offsetArray.<number>

Offset.

setPosition(position)

Set the position for this overlay.

NameTypeDescription
positionol.Coordinate | undefined

The spatial point that the overlay is anchored at.

setPositioning(positioning)

Set the positioning for this overlay.

NameTypeDescription
positioningol.OverlayPositioning

how the overlay is positioned relative to its point on the map.

un(type, listener, opt_this) inherited

Unlisten for a certain type of event.

NameTypeDescription
typestring | Array.<string>

The event type or array of event types.

listenerfunction

The listener function.

thisObject

The object which was used as this by the listener.

Removes an event listener using the key returned by on() or once().

NameTypeDescription
keygoog.events.Key

Key.

上面的内容是OL3 的API中关于overlay的部分。


调用演示样例:

1、popup样式

		body, #map {
			border: 0px;
			margin: 0px;
			padding: 0px;
			 100%;
			height: 100%;
			font-size: 13px;
		}

		.ol-popup {
			display: none;
			position: absolute;
			background-color: white;
			-moz-box-shadow: 0 1px 4px rgba(0,0,0,0.2);
			-webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
			filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
			border: 1px solid #cccccc;
			bottom: 12px;
			left: -50px;
			 200px;
		}
		.ol-popup:after, .ol-popup:before {
			top: 100%;
			border: solid transparent;
			content: " ";
			height: 0;
			 0;
			position: absolute;
			pointer-events: none;
		}
		.ol-popup:after {
			border-top-color: white;
			border- 10px;
			left: 48px;
			margin-left: -10px;
		}
		.ol-popup:before {
			border-top-color: #cccccc;
			border- 11px;
			left: 48px;
			margin-left: -11px;
		}
		.popup-title{
			font-weight: bold;
			border-bottom:1px solid #cccccc;
			padding: 5px 8px;
		}
		.popup-content{
			padding: 5px 8px;
		}
		.ol-popup-closer {
			text-decoration: none;
			position: absolute;
			top: 6px;
			right: 6px;
		}
		.ol-popup-closer:after {
			content: "✖";
		}
2、popup容器

<div id="map">
	<div   class="ol-popup">
		<a href="https://tool.4xseo.com/article/241694.html"   class="ol-popup-closer"></a>
		<div   class="popup-title"></div>
		<div   class="popup-content"></div>
	</div>
</div>
3、实现js

			var container = document.getElementById('popup');
			var content = document.getElementById('popup-content');
			var title = document.getElementById('popup-title');
			var closer = document.getElementById('popup-closer');
			closer.onclick = function(){
				container.style.display = 'none';
				closer.blur();
				return false;
			};
			var overlay = new ol.Overlay({
				element: container
			});
			map.addOverlay(overlay);
			map.on('click', function(evt) {
				var coordinate = evt.coordinate;
				var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
						coordinate, 'EPSG:4326', 'EPSG:4326'));
				overlay.setPosition(coordinate);
				content.innerHTML = '<p>You clicked here:</p><code>' + hdms +
				'</code>';
				container.style.display = 'block';
				title.innerHTML = "提示信息";
				title.style.display = 'block';
				map.getView().setCenter(coordinate);
			});

演示样例完整代码例如以下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Ol3 popup</title>
	<link rel="stylesheet" type="text/css" href="http://localhost/ol3/css/ol.css"/>
	<style type="text/css">
		body, #map {
			border: 0px;
			margin: 0px;
			padding: 0px;
			 100%;
			height: 100%;
			font-size: 13px;
		}

		.ol-popup {
			display: none;
			position: absolute;
			background-color: white;
			-moz-box-shadow: 0 1px 4px rgba(0,0,0,0.2);
			-webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
			filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));
			border: 1px solid #cccccc;
			bottom: 12px;
			left: -50px;
			 200px;
		}
		.ol-popup:after, .ol-popup:before {
			top: 100%;
			border: solid transparent;
			content: " ";
			height: 0;
			 0;
			position: absolute;
			pointer-events: none;
		}
		.ol-popup:after {
			border-top-color: white;
			border- 10px;
			left: 48px;
			margin-left: -10px;
		}
		.ol-popup:before {
			border-top-color: #cccccc;
			border- 11px;
			left: 48px;
			margin-left: -11px;
		}
		.popup-title{
			font-weight: bold;
			border-bottom:1px solid #cccccc;
			padding: 5px 8px;
		}
		.popup-content{
			padding: 5px 8px;
		}
		.ol-popup-closer {
			text-decoration: none;
			position: absolute;
			top: 6px;
			right: 6px;
		}
		.ol-popup-closer:after {
			content: "✖";
		}
	</style>
	<script type="text/javascript" src="http://localhost/ol3/build/ol.js"></script>
	<script type="text/javascript" src="http://localhost/jquery/jquery-1.8.3.js"></script>
	<script type="text/javascript">
		function init(){
			var format = 'image/png';
			var bounds = [73.4510046356223, 18.1632471876417,
				134.976797646506, 53.5319431522236];
			var untiled = new ol.layer.Image({
				source: new ol.source.ImageWMS({
					ratio: 1,
					url: 'http://localhost:8081/geoserver/lzugis/wms',
					params: {'FORMAT': format,
						'VERSION': '1.1.1',
						LAYERS: 'lzugis:capital',
						STYLES: ''
					}
				})
			});
			var projection = new ol.proj.Projection({
				code: 'EPSG:4326',
				units: 'degrees'
			});
			var container = document.getElementById('popup');
			var content = document.getElementById('popup-content');
			var title = document.getElementById('popup-title');
			var closer = document.getElementById('popup-closer');
			closer.onclick = function(){
				container.style.display = 'none';
				closer.blur();
				return false;
			};
			var overlay = new ol.Overlay({
				element: container
			});
			map.addOverlay(overlay);

			var map = new ol.Map({
				controls: ol.control.defaults({
					attribution: false
				}),
				target: 'map',
				layers: [untiled],
				overlays: [overlay],
				view: new ol.View({
					projection: projection
				})
			});
			map.getView().fitExtent(bounds, map.getSize());

			map.on('click', function(evt) {
				var coordinate = evt.coordinate;
				var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
						coordinate, 'EPSG:4326', 'EPSG:4326'));
				overlay.setPosition(coordinate);
				content.innerHTML = '<p>You clicked here:</p><code>' + hdms +
				'</code>';
				container.style.display = 'block';
				title.innerHTML = "提示信息";
				title.style.display = 'block';
				map.getView().setCenter(coordinate);
			});
		}
	</script>
</head>
<body onLoad="init()">
<div id="map">
	<div   class="ol-popup">
		<a href="https://tool.4xseo.com/article/241694.html"   class="ol-popup-closer"></a>
		<div   class="popup-title"></div>
		<div   class="popup-content"></div>
	</div>
</div>
</body>
</html>

实现后的效果例如以下:

OpenLayers3基础教程——OL3之Popup第1张


相关文章:

OpenLayers3基础教程——OL3基本概念

OpenLayers3基础教程——载入资源

OpenLayers3基础教程——OL3 介绍control


OpenLayers3基础教程——OL3之Popup第2张





免责声明:文章转载自《OpenLayers3基础教程——OL3之Popup》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇一本通例题-生日蛋糕——题解&amp;lt;超强深搜剪枝,从无限到有限&amp;gt;Kafka如何实现高性能io?下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

web前端常见的加密算法介绍

如今,信息安全越来越受到重视,前端的各种加密变得更加重要。目前,常见的加密算法可分为三种类型的对称加密算法:AES,…不对称加密算法:RSA,…哈希算法:MD5,…对称加密算法对称加密是指使用相同密钥进行加密和解密的加密算法。如果一方的密钥被泄露,加密的信息将是不安全的。使用场景:AESAES用于本地数据加密、https通信、网络传输等:高级加密标准是最常见...

小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors

在引用组件的组件和页面中使用后代选择器在某些极端情况下会产生意想不到的性能。如果是,请避免使用它们。子元素选择器只能在视图组件及其子节点之间使用,其他组件可能会导致意外情况。继承的样式(如字体和颜色)将从组件外部继承到组件内部。除了继承样式之外,app.wxss中的样式和组件所在页面的样式对于自定义组件无效。...

试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

解决方法:iis应用程序池--˃高级设置--˃启用32位应用程序˂!body{font-family:"Verdana";font-weight:normal;font-size:.7em;color:black;}p{font-family:"Verdana";font-weight:normal;color:black;margin-top:-5px}b...

(二)Jenkins配置主从节点实例

4.从节点配置和相关配置中从节点机创建jenkins用户,并从一些环境配置中创建jenkings用户的ssh密钥,用于指定上述配置界面的ssh启动模式;在配置启动模式和项目源代码管理中从远程仓库获取源代码;创建Jenkins用户并使用root登录到远程子节点计算机。#adduserjenkins#passwdjenkins生成Jenkins用户的ssh密钥。...

安装samba服务器实现Linux mint和Windows共享文件

安装samba服务器以实现Linuxmint和Windows共享文件。在Linuxmint普通用户下执行命令:sudoapt-geinstallsamba、installsamba和打开smb。conf配置文件,并执行命令gedit/etc/samba/smb-Coff,如果您想安装gedit(sudoapt-geinstallgedit),还可以使用Lin...

Grafana 安装配置启动

多个数据源:Graphite、InfluxDB、OpenTSDB、Prometheus、Elasticsearch、CloudWatch、KairosDB、Zabbix等。通知和提醒,达到目标设置的阈值,并发出警报。grafana具有以下三个用户权限管理员:超级管理员,具有所有权限查看器:只能查看DashBoardEditer:无法创建用户,无法添加数据源,...