简体   繁体   English

添加多个弹出标记 - Openlayers map

[英]Adding more than one popup marker - Openlayers map

I have created a marker with a popup using openlayers.我使用 openlayers 创建了一个带有弹出窗口的标记。 It all works fine but I cen't figure out how to add another marker in a different location.一切正常,但我不知道如何在不同的位置添加另一个标记。 Here is my JS:这是我的 JS:

var marker = new ol.layer.Vector({
    source: new ol.source.Vector({
        features: [
        new ol.Feature({
            geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.123666, 52.504650]))
        })
        ]
    })
});
var container = document.getElementById('popup');
 var content = document.getElementById('popup-content');
 var closer = document.getElementById('popup-closer');

 var overlay = new ol.Overlay({
     element: container,
     autoPan: true,
     autoPanAnimation: {
         duration: 250
     }
 });
 map.addOverlay(overlay);

 closer.onclick = function() {
     overlay.setPosition(undefined);
     closer.blur();
     return false;
 };
 
 map.on('singleclick', function (event) {
     if (map.hasFeatureAtPixel(event.pixel) === true) {
         var coordinate = event.coordinate;

         content.innerHTML = '<b>Barrow Hill and Tansey Green</b><br />I am a popup.';
         overlay.setPosition(coordinate);
     } else {
         overlay.setPosition(undefined);
         closer.blur();
     }
 });

and my html:和我的 html:

<div id="mymap">
<div id="popup" class="ol-popup">
     <a href="#" id="popup-closer" class="ol-popup-closer"></a>
     <div id="popup-content"></div>

There are may ways to do this.有多种方法可以做到这一点。 To add multiple markers based on your existing code, add additional features to the array of features, with an additional property for the popup content (assuming you want it to vary by the location):要根据现有代码添加多个标记,请向功能数组添加其他功能,并为popup内容添加一个附加属性(假设您希望它因位置而异):

var marker = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [
      new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.123666, 52.504650])),
        html: '<b>Barrow Hill and Tansey Green</b><br />I am a popup.'
      }),
      new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.12, 52.504])),
        html: '<b>Marker 2</b><br />I am a popup.'
      }),
      new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.13, 52.51])),
        html: '<b>Marker 3</b><br />I am a popup.'
      })
    ]
  })
});

Then change your popup opener function to find the data from the clicked location and display it in the popup:然后更改您的弹出窗口打开器 function 以从单击的位置查找数据并将其显示在弹出窗口中:

map.on('singleclick', function(event) {
  if (map.hasFeatureAtPixel(event.pixel) === true) {
    var coordinate = event.coordinate;
    var features = map.getFeaturesAtPixel(event.pixel);
    content.innerHTML = features[0].getProperties().html;
    overlay.setPosition(coordinate);
  } else {
    overlay.setPosition(undefined);
    closer.blur();
  }
});

proof of concept fiddle概念证明小提琴

显示弹出窗口的地图屏幕截图

code snippet:代码片段:

 var marker = new ol.layer.Vector({ source: new ol.source.Vector({ features: [ new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.123666, 52.504650])), html: '<b>Barrow Hill and Tansey Green</b><br />I am a popup.' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.12, 52.504])), html: '<b>Marker 2</b><br />I am a popup.' }), new ol.Feature({ geometry: new ol.geom.Point(ol.proj.fromLonLat([-2.13, 52.51])), html: '<b>Marker 3</b><br />I am a popup.' }) ] }) }); //OSM layer creation var OSMmap = new ol.layer.Tile({ // TileLayer({ source: new ol.source.OSM(), }); //Map initiation var map = new ol.Map({ target: 'mymap', layers: [OSMmap, marker], //cities], view: new ol.View({ center: ol.proj.fromLonLat([15, 0]), zoom: 2 }), }); var container = document.getElementById('popup'); var content = document.getElementById('popup-content'); var closer = document.getElementById('popup-closer'); var overlay = new ol.Overlay({ element: container, autoPan: true, autoPanAnimation: { duration: 250 } }); map.addOverlay(overlay); closer.onclick = function() { overlay.setPosition(undefined); closer.blur(); return false; }; map.on('singleclick', function(event) { if (map.hasFeatureAtPixel(event.pixel) === true) { var coordinate = event.coordinate; var features = map.getFeaturesAtPixel(event.pixel); content.innerHTML = features[0].getProperties().html; overlay.setPosition(coordinate); } else { overlay.setPosition(undefined); closer.blur(); } }); map.getView().fit(marker.getSource().getExtent(), { padding: [40, 16, 40, 16] });
 /* Always set the map height explicitly to define the size of the div * element that contains the map. */ #mymap { height: 100%; } /* Optional: Makes the sample page fill the window. */ html, body { height: 100%; margin: 0; padding: 0; } #popup { background: #FFFFFF; border: black 1px solid; }
 <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script> <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script> <link rel="stylesheet" href="https://openlayers.org/en/v6.5.0/css/ol.css" type="text/css"> <div id="mymap"> <div id="popup" class="ol-popup"> <a href="#" id="popup-closer" class="ol-popup-closer"></a> <div id="popup-content"></div> </div> </div>

Another way to do it 另一种方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM