簡體   English   中英

使用OpenLayer3顯示標記,彈出窗口

[英]Display Markers, Popups with OpenLayer3

我試圖了解如何在openmers3上顯示osm地圖上的標記/彈出窗口,我在ol3網頁上的示例中找到了示例,但是......

是否有更多使用javascript或jquery編碼標記/彈出窗口的示例,最好是像這樣或類似的例子。

這個想法是標記一個建築物,通過點擊標記它將彈出建築物的一些信息,更多我想連接從城市到這個建築物的重要地方(圖書館,餐館,公共汽車站等)。 我希望如果有人能解釋我如何開始構建這個,我不想使用bootstrap3(我已經看過覆蓋示例),而是想要純html5,css3,javascript / jquery)

您可以使用純HTML,CSS和JavaScript創建彈出窗口,如彈出窗口示例所示。 您想要的完整工作示例如下: http//jsfiddle.net/ahocevar/z86zc9fz/1/

彈出窗口的HTML很簡單:

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

CSS更復雜:

.ol-popup {
  position: absolute;
  background-color: white;
  -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));
  padding: 15px;
  border-radius: 10px;
  border: 1px solid #cccccc;
  bottom: 12px;
  left: -50px;
  min-width: 80px;
}
.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;
}
.ol-popup-closer {
  text-decoration: none;
  position: absolute;
  top: 2px;
  right: 8px;
}
.ol-popup-closer:after {
  content: "✖";
}

要制作彈出窗口,請使用ol.Overlay

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

var closer = document.getElementById('popup-closer');
closer.onclick = function() {
  overlay.setPosition(undefined);
  closer.blur();
  return false;
};

要使功能可單擊,請使用

var content = document.getElementById('popup-content');
map.on('singleclick', function(evt) {
  var name = map.forEachFeatureAtPixel(evt.pixel, function(feature) {
    return feature.get('name');
  });
  var coordinate = evt.coordinate;
  content.innerHTML = name;
  overlay.setPosition(coordinate);
});

對於指針在功能上方時的視覺反饋,請使用

map.on('pointermove', function(evt) {
  map.getTargetElement().style.cursor = map.hasFeatureAtPixel(evt.pixel) ? 'pointer' : '';
});

特征(即標記)來自矢量圖層:

var markers = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [
      new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([16.37, 48.2])),
        name: 'Vienna',
        type: 'City'
      }),
      new ol.Feature({
        geometry: new ol.geom.Point(ol.proj.fromLonLat([-0.13, 51.51])),
        name: 'London',
        type: 'City'
      })
    ]
  }),
  style: new ol.style.Style({
    image: new ol.style.Icon({
      src: '//openlayers.org/en/v3.12.1/examples/data/icon.png',
      anchor: [0.5, 1]
    })
  })
});
map.addLayer(markers);

上面的代碼段使用固定樣式,即所有類型的功能都使用相同的圖標。 如果您有不同類型的功能,則可以定義樣式函數而不是固定樣式:

var cityStyle = new ol.style.Style({
  image: new ol.style.Icon({
    src: '//openlayers.org/en/v3.12.1/examples/data/icon.png',
    anchor: [0.5, 1]
  })
});
var otherStyle = new ol.style.Style({
  image: new ol.style.Icon({
    src: '//openlayers.org/en/v3.12.1/examples/data/Butterfly.png'
  })
});
var markers = new ol.layer.Vector({
  // ...
  style: function(feature, resolution) {
    if (feature.get('type') == 'City' {
      return cityStyle;
    }
    return otherStyle;
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM