(转)OpenLayers3基础教程——OL3之Popup
http://blog.csdn.net/gisshixisheng/article/details/46794813
概述:
本节重点讲述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)
Name | Type | Description | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options
|
Overlay options.
|
Fires:
change:element
(ol.ObjectEvent)change:map
(ol.ObjectEvent)change:offset
(ol.ObjectEvent)change:position
(ol.ObjectEvent)change:positioning
(ol.ObjectEvent)
Extends
- ol.Object
Observable Properties
Name | Type | Settable | ol.ObjectEvent type | Description |
---|---|---|---|---|
element
|
Element | undefined | yes |
change:element
|
The Element containing the overlay. |
map
|
ol.Map | undefined | yes |
change:map
|
The map that the overlay is part of. |
offset
|
Array.<number> | yes |
change:offset
|
The offset. |
position
|
ol.Coordinate | undefined | yes |
change:position
|
The spatial point that the overlay is anchored at. |
positioning
|
ol.OverlayPositioning | yes |
change: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.
-
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.
getPositioning(){ol.OverlayPositioning}
-
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.
Name Type Description type
string | Array.<string> The event type or array of event types.
listener
function The listener function.
this
Object The object to use as
this
inlistener
.Returns:
Unique key for the listener.
once(type, listener, opt_this){goog.events.Key} inherited
-
Listen once for a certain type of event.
Name Type Description type
string | Array.<string> The event type or array of event types.
listener
function The listener function.
this
Object The object to use as
this
inlistener
.Returns:
Unique key for the listener.
setElement(element)
-
Set the DOM element to be associated with this overlay.
Name Type Description element
Element | undefined The Element containing the overlay.
setMap(map)
-
Set the map to be associated with this overlay.
Name Type Description map
ol.Map | undefined The map that the overlay is part of.
setOffset(offset)
-
Set the offset for this overlay.
Name Type Description offset
Array.<number> Offset.
setPosition(position)
-
Set the position for this overlay.
Name Type Description position
ol.Coordinate | undefined The spatial point that the overlay is anchored at.
setPositioning(positioning)
-
Set the positioning for this overlay.
Name Type Description positioning
ol.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.
Name Type Description type
string | Array.<string> The event type or array of event types.
listener
function The listener function.
this
Object The object which was used as
this
by thelistener
.
unByKey(key) inherited
-
Removes an event listener using the key returned by
on()
oronce()
.Name Type Description key
goog.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>
实现后的效果如下:
转载于:https://www.cnblogs.com/telwanggs/p/6972905.html
(转)OpenLayers3基础教程——OL3之Popup相关推荐
- OpenLayers3基础教程——OL3之Popup
概述: 本节重点讲述OpenLayers3中Popup的调用时实现,OL3改用Overlay取代OL2的Popup功能. 接口简单介绍: overlay跟ol.control.Control一样,是一 ...
- (转)OpenLayers3基础教程——OL3基本概念
http://blog.csdn.net/gisshixisheng/article/details/46756275 OpenLayers3基础教程--OL3基本概念 从本节开始,我会陆陆续续的更新 ...
- OpenLayers3基础教程——OL3 介绍control
概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 control比较: 相比较Ol2的control,OL3显得特别少,下图分别为Ol2和Ol3的control: Ol2的c ...
- OpenLayers3基础教程——OL3基本概念
从本节开始,我会陆陆续续的更新有关OL3的相关文章--OpenLayers3基础教程,欢迎大家关注我的博客,同时也希望我的博客能够给大家带来一点帮助. 概述: OpenLayers 3对OpenLay ...
- (转) OpenLayers3基础教程——OL3 介绍control
http://blog.csdn.net/gisshixisheng/article/details/46761535 概述: 本文讲述的是Ol3中的control的介绍和应用. OL2和OL3 co ...
- (转)OpenLayers3基础教程——OL3 介绍interaction
http://blog.csdn.net/gisshixisheng/article/details/46808647 概述: 本节主要讲述OL3的交互操作interaction,重点介绍draw,s ...
- (转) OpenLayers3基础教程——加载资源
概述: 本节讲述如何在Ol3中加载wms图层并显示到地图中. Ol3下载: 你可以在OL官网去下载,下载地址为http://openlayers.org/download/,也可以去我的百度云盘下载, ...
- Chrome扩展开发基础教程(附HelloWorld)
1 概述 Chrome扩展开发的基础教程,代码基于原生JS+H5,教程内容基于谷歌扩展开发官方文档. 2 环境 Chrome 88.0.4324.96 Chromium 87.0.4280.141 B ...
- Spring Cloud Alibaba基础教程:使用Nacos实现服务注册与发现
自Spring Cloud Alibaba发布第一个Release以来,就备受国内开发者的高度关注.虽然Spring Cloud Alibaba还没能纳入Spring Cloud的主版本管理中,但是凭 ...
最新文章
- AI 助力金融后,中美金融科技领域最大的差距是什么?
- 由《我也能做CTO》引起的作者与读者的交流
- Oracle取最大/最小值函数
- PSPNet-tensorflow实现并训练数据
- android xml java混合编程_Android | 自动调整文本大小的 TextViews
- Nginx中的break和last
- ArrayList 使用迭代抛出ConcurrentModificationException解决方法
- Prototype使用$$()函数
- PHP一维数组转二维数组正则表达式
- moment.js的方法总结
- MATLAB与高等数学--dsolve命令
- 爬虫 爬取豆瓣高分电影信息
- bc vc投资_天使投资、VC 以及 PE 的区别是什么?
- AndroidIos打包
- C语言程序设计教程(第三版)课后习题6.7
- 用Python解决数据结构与算法问题(一):Python基础
- 目录打开显示提示文件或目录损坏且无法读取、文件或目录损坏且无法读取的破解之道
- 一文搞懂指针,指针的指针,悬浮指针,野指针
- 大学excel题库含答案_Excel题库(附答案).doc
- python语言合法命名有哪些-以下不是 Python 语言合法命名的是 :( )_学小易找答案...
热门文章
- wireshark协议插件开发--官方文档中文翻译
- 软连接与硬链接的区别,以及如何删除软链接
- (41)FPGA四种常用逻辑门(异或门)
- (40)Verilog HDL锁存器设计
- android qt 串口通信,Qt串口通信开发之QSerialPort模块详细使用方法与实例
- linux编译测试代码,rtc在linux上的测试代码
- diag开关什么意思_1P空气开关便宜、好用,为什么电工师傅却要我们买2P空气开关?...
- 动态链接库的问题解决
- 小米更新显示非官方rom_魔趣ROM 安装刷入教程(小白新手)
- linux下的进程间通信-管道及共享内存