【第22期】观点:IT 行业加班,到底有没有价值?

OpenLayers3基础教程——OL3之Popup

原创 2015年07月07日 22:23:00

概述:

本节重点讲述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;
			width: 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;
			width: 200px;
		}
		.ol-popup:after, .ol-popup:before {
			top: 100%;
			border: solid transparent;
			content: " ";
			height: 0;
			width: 0;
			position: absolute;
			pointer-events: none;
		}
		.ol-popup:after {
			border-top-color: white;
			border-width: 10px;
			left: 48px;
			margin-left: -10px;
		}
		.ol-popup:before {
			border-top-color: #cccccc;
			border-width: 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 id="popup" class="ol-popup">
		<a href="#" id="popup-closer" class="ol-popup-closer"></a>
		<div id="popup-title" class="popup-title"></div>
		<div id="popup-content" 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;
			width: 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;
			width: 200px;
		}
		.ol-popup:after, .ol-popup:before {
			top: 100%;
			border: solid transparent;
			content: " ";
			height: 0;
			width: 0;
			position: absolute;
			pointer-events: none;
		}
		.ol-popup:after {
			border-top-color: white;
			border-width: 10px;
			left: 48px;
			margin-left: -10px;
		}
		.ol-popup:before {
			border-top-color: #cccccc;
			border-width: 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 id="popup" class="ol-popup">
		<a href="#" id="popup-closer" class="ol-popup-closer"></a>
		<div id="popup-title" class="popup-title"></div>
		<div id="popup-content" class="popup-content"></div>
	</div>
</div>
</body>
</html>

实现后的效果如下:



相关文章:

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

OpenLayers3基础教程——加载资源

OpenLayers3基础教程——OL3 介绍control








版权声明:本文为LZUGIS原创文章,未经允许不得转载。 举报

相关文章推荐

OpenLayers3基础教程——OL3 介绍control

本文讲述的是Ol3中的control的介绍和应用。

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

OpenLayers 3对OpenLayers网络地图库进行了根本的重新设计。版本2虽然被广泛使用,但从JavaScript开发的早期发展阶段开始,已日益现实出它的落后。 OL3已运用现代的设计模式从...

程序员升职加薪指南!还缺一个“证”!

CSDN出品,立即查看!

OpenLayers3基础教程——加载资源

本节讲述如何在Ol3中加载wms图层并显示到地图中。

OpenLayers3基础教程——OL3 介绍interaction

本节主要讲述OL3的交互操作interaction,重点介绍draw,select以及modify。

OpenLayers 3 之 绘制图形(ol.interaction.Draw)详解

在地图上绘制各种各样的矢量图形,很多应用场景都需要用到,比如可以实现在客户端勾绘矢量图形并将结果保存到数据库以待后续编辑和可视化等等很多有用的功能。这次我们看看怎么利用 OpenLayers 3 绘制...

OpenLayers 3 之 弹出框(popup)

摘要          上回说到,加载完地图后,我们想更多一点交互,而不仅仅是缩放和平移,赋予地图多一点的生命 ~-~!点击地图,它至少应该知道我是在哪里点击了,并且弹出一个气球类似的框框告诉我!这次...

OpenLayers 3 之 绘制图形(ol.interaction.Draw)原理解析

在 OpenLayers 3 中,负责交互的模块中,有一个负责绘制图形的交互模块,这个交互子模块是 ol.interaction.Draw。该模块允许用户通过鼠标点击(PC浏览器环境)或者手指触摸( ...

OpenLayers 3 之 Overlay(覆盖物)详解

Overlay 从名字看,是覆盖图、覆盖物的意思,主要的用途就是在地图之上再覆盖一层,用以显示额外的可见元素,可见元素一般是 HTML 元素,利用 overlay,可以将可见元素放置到地图的任意位置,...

OpenLayers 3 之 地图矢量图层(ol.layer.Vector)详解

在 GIS 中,地图一般分为两大类:栅格地图和矢量地图,栅格地图其实就是数码照片,只不过有的是卫星拍的。它们有一个共同特征,就是它们都是由很多个像素组成,像素大小是一致的,行高和列宽是一致的,从这个角...

OpenLayers 3 之加载地图

摘要          一个加载地图的小例子,分析原理,并解析源码! 正文 例程如下: .map { height: 400px; ...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)