• (转)OpenLayers3基础教程——OL3之Popup


    http://blog.csdn.net/gisshixisheng/article/details/46794813

    概述:

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

    接口简介:

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

    [javascript] view plain copy
     
     print?
    1. var popup = new ol.Overlay({  
    2.   element: document.getElementById('popup')  
    3. });  
    4. popup.setPosition(coordinate);  
    5. map.addOverlay(popup);  

    new ol.Overlay(options)

    NameTypeDescription
    options

    Overlay options.

    NameTypeDescription
    element Element | undefined

    The overlay element.

    offset Array.<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].

    position ol.Coordinate |undefined

    The overlay position in map projection.

    positioning ol.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'.

    stopEvent boolean | 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.

    insertFirst boolean | 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
    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. 

    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
    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 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
    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 in listener.

    Returns:
    Unique key for the listener. 

    setElement(element)

    Set the DOM element to be associated with this overlay.

    NameTypeDescription
    element Element | undefined

    The Element containing the overlay.

    setMap(map)

    Set the map to be associated with this overlay.

    NameTypeDescription
    map ol.Map | undefined

    The map that the overlay is part of.

    setOffset(offset)

    Set the offset for this overlay.

    NameTypeDescription
    offset Array.<number>

    Offset.

    setPosition(position)

    Set the position for this overlay.

    NameTypeDescription
    position ol.Coordinate | undefined

    The spatial point that the overlay is anchored at.

    setPositioning(positioning)

    Set the positioning for this overlay.

    NameTypeDescription
    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.

    NameTypeDescription
    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 the listener.

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

    NameTypeDescription
    key goog.events.Key

    Key.

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

    调用示例:

    1、popup样式

    [css] view plain copy
     
     print?
    1. body, #map {  
    2.     border: 0px;  
    3.     margin: 0px;  
    4.     padding: 0px;  
    5.      100%;  
    6.     height: 100%;  
    7.     font-size: 13px;  
    8. }  
    9.   
    10. .ol-popup {  
    11.     display: none;  
    12.     position: absolute;  
    13.     background-color: white;  
    14.     -moz-box-shadow: 0 1px 4px rgba(0,0,0,0.2);  
    15.     -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));  
    16.     filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));  
    17.     border: 1px solid #cccccc;  
    18.     bottom: 12px;  
    19.     left: -50px;  
    20.      200px;  
    21. }  
    22. .ol-popup:after, .ol-popup:before {  
    23.     top: 100%;  
    24.     border: solid transparent;  
    25.     content: " ";  
    26.     height: 0;  
    27.      0;  
    28.     position: absolute;  
    29.     pointer-events: none;  
    30. }  
    31. .ol-popup:after {  
    32.     border-top-color: white;  
    33.     border- 10px;  
    34.     left: 48px;  
    35.     margin-left: -10px;  
    36. }  
    37. .ol-popup:before {  
    38.     border-top-color: #cccccc;  
    39.     border- 11px;  
    40.     left: 48px;  
    41.     margin-left: -11px;  
    42. }  
    43. .popup-title{  
    44.     font-weight: bold;  
    45.     border-bottom:1px solid #cccccc;  
    46.     padding: 5px 8px;  
    47. }  
    48. .popup-content{  
    49.     padding: 5px 8px;  
    50. }  
    51. .ol-popup-closer {  
    52.     text-decoration: none;  
    53.     position: absolute;  
    54.     top: 6px;  
    55.     right: 6px;  
    56. }  
    57. .ol-popup-closer:after {  
    58.     content: "✖";  
    59. }  

    2、popup容器

    [html] view plain copy
     
     print?
    1. <div id="map">  
    2.     <div id="popup" class="ol-popup">  
    3.         <href="#" id="popup-closer" class="ol-popup-closer"></a>  
    4.         <div id="popup-title" class="popup-title"></div>  
    5.         <div id="popup-content" class="popup-content"></div>  
    6.     </div>  
    7. </div>  

    3、实现js

    [javascript] view plain copy
     
     print?
    1. var container = document.getElementById('popup');  
    2. var content = document.getElementById('popup-content');  
    3. var title = document.getElementById('popup-title');  
    4. var closer = document.getElementById('popup-closer');  
    5. closer.onclick = function(){  
    6.     container.style.display = 'none';  
    7.     closer.blur();  
    8.     return false;  
    9. };  
    10. var overlay = new ol.Overlay({  
    11.     element: container  
    12. });  
    13. map.addOverlay(overlay);  
    [javascript] view plain copy
     
     print?
    1. map.on('click', function(evt) {  
    2.     var coordinate = evt.coordinate;  
    3.     var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(  
    4.             coordinate, 'EPSG:4326', 'EPSG:4326'));  
    5.     overlay.setPosition(coordinate);  
    6.     content.innerHTML = '<p>You clicked here:</p><code>' + hdms +  
    7.     '</code>';  
    8.     container.style.display = 'block';  
    9.     title.innerHTML = "提示信息";  
    10.     title.style.display = 'block';  
    11.     map.getView().setCenter(coordinate);  
    12. });  


    示例完整代码如下:

    [html] view plain copy
     
     print?
    1. <html xmlns="http://www.w3.org/1999/xhtml">  
    2. <head>  
    3.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    4.     <title>Ol3 popup</title>  
    5.     <link rel="stylesheet" type="text/css" href="http://localhost/ol3/css/ol.css"/>  
    6.     <style type="text/css">  
    7.         body, #map {  
    8.             border: 0px;  
    9.             margin: 0px;  
    10.             padding: 0px;  
    11.              100%;  
    12.             height: 100%;  
    13.             font-size: 13px;  
    14.         }  
    15.   
    16.         .ol-popup {  
    17.             display: none;  
    18.             position: absolute;  
    19.             background-color: white;  
    20.             -moz-box-shadow: 0 1px 4px rgba(0,0,0,0.2);  
    21.             -webkit-filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));  
    22.             filter: drop-shadow(0 1px 4px rgba(0,0,0,0.2));  
    23.             border: 1px solid #cccccc;  
    24.             bottom: 12px;  
    25.             left: -50px;  
    26.              200px;  
    27.         }  
    28.         .ol-popup:after, .ol-popup:before {  
    29.             top: 100%;  
    30.             border: solid transparent;  
    31.             content: " ";  
    32.             height: 0;  
    33.              0;  
    34.             position: absolute;  
    35.             pointer-events: none;  
    36.         }  
    37.         .ol-popup:after {  
    38.             border-top-color: white;  
    39.             border- 10px;  
    40.             left: 48px;  
    41.             margin-left: -10px;  
    42.         }  
    43.         .ol-popup:before {  
    44.             border-top-color: #cccccc;  
    45.             border- 11px;  
    46.             left: 48px;  
    47.             margin-left: -11px;  
    48.         }  
    49.         .popup-title{  
    50.             font-weight: bold;  
    51.             border-bottom:1px solid #cccccc;  
    52.             padding: 5px 8px;  
    53.         }  
    54.         .popup-content{  
    55.             padding: 5px 8px;  
    56.         }  
    57.         .ol-popup-closer {  
    58.             text-decoration: none;  
    59.             position: absolute;  
    60.             top: 6px;  
    61.             right: 6px;  
    62.         }  
    63.         .ol-popup-closer:after {  
    64.             content: "✖";  
    65.         }  
    66.     </style>  
    67.     <script type="text/javascript" src="http://localhost/ol3/build/ol.js"></script>  
    68.     <script type="text/javascript" src="http://localhost/jquery/jquery-1.8.3.js"></script>  
    69.     <script type="text/javascript">  
    70.         function init(){  
    71.             var format = 'image/png';  
    72.             var bounds = [73.4510046356223, 18.1632471876417,  
    73.                 134.976797646506, 53.5319431522236];  
    74.             var untiled = new ol.layer.Image({  
    75.                 source: new ol.source.ImageWMS({  
    76.                     ratio: 1,  
    77.                     url: 'http://localhost:8081/geoserver/lzugis/wms',  
    78.                     params: {'FORMAT': format,  
    79.                         'VERSION': '1.1.1',  
    80.                         LAYERS: 'lzugis:capital',  
    81.                         STYLES: ''  
    82.                     }  
    83.                 })  
    84.             });  
    85.             var projection = new ol.proj.Projection({  
    86.                 code: 'EPSG:4326',  
    87.                 units: 'degrees'  
    88.             });  
    89.             var container = document.getElementById('popup');  
    90.             var content = document.getElementById('popup-content');  
    91.             var title = document.getElementById('popup-title');  
    92.             var closer = document.getElementById('popup-closer');  
    93.             closer.onclick = function(){  
    94.                 container.style.display = 'none';  
    95.                 closer.blur();  
    96.                 return false;  
    97.             };  
    98.             var overlay = new ol.Overlay({  
    99.                 element: container  
    100.             });  
    101.             map.addOverlay(overlay);  
    102.   
    103.             var map = new ol.Map({  
    104.                 controls: ol.control.defaults({  
    105.                     attribution: false  
    106.                 }),  
    107.                 target: 'map',  
    108.                 layers: [untiled],  
    109.                 overlays: [overlay],  
    110.                 view: new ol.View({  
    111.                     projection: projection  
    112.                 })  
    113.             });  
    114.             map.getView().fitExtent(bounds, map.getSize());  
    115.   
    116.             map.on('click', function(evt) {  
    117.                 var coordinate = evt.coordinate;  
    118.                 var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(  
    119.                         coordinate, 'EPSG:4326', 'EPSG:4326'));  
    120.                 overlay.setPosition(coordinate);  
    121.                 content.innerHTML = '<p>You clicked here:</p><code>' + hdms +  
    122.                 '</code>';  
    123.                 container.style.display = 'block';  
    124.                 title.innerHTML = "提示信息";  
    125.                 title.style.display = 'block';  
    126.                 map.getView().setCenter(coordinate);  
    127.             });  
    128.         }  
    129.     </script>  
    130. </head>  
    131. <body onLoad="init()">  
    132. <div id="map">  
    133.     <div id="popup" class="ol-popup">  
    134.         <href="#" id="popup-closer" class="ol-popup-closer"></a>  
    135.         <div id="popup-title" class="popup-title"></div>  
    136.         <div id="popup-content" class="popup-content"></div>  
    137.     </div>  
    138. </div>  
    139. </body>  
    140. </html>  

    实现后的效果如下:

  • 相关阅读:
    鸽巢原理 学习笔记
    POJ 1811 Prime Test
    Ubuntu下pdf乱码问题解决方法
    POJ 基础数学
    SRM遇到的一个数论技巧——最大公约数和最小公倍数的关系
    计算几何初步模板
    矩阵快速幂 学习笔记
    ZOJ 2849 Attack of Panda Virus (优先队列 priority_queue)
    欧几里德算法和扩展欧几里德算法
    记部分HASH函数
  • 原文地址:https://www.cnblogs.com/telwanggs/p/6972905.html
Copyright © 2020-2023  润新知