javascript - 使用 OpenLayer3 显示标记、弹出窗口

标签 javascript jquery html openstreetmap openlayers-3

我试图了解如何使用 openlayers3 在 osm map 上显示标记/弹出窗口,我在 ol3 网页的示例中找到了示例,但是...

是否有更多使用 javascript 或 jquery 编写标记/弹出窗口的示例,最好是类似 this 的示例或类似的例子。

这个想法是标记一个建筑物,通过点击标记它会弹出一些建筑物的信息,此外我想将城市中的重要地点连接到这个建筑物(图书馆,餐馆,公共(public)汽车站等) .我希望有人可以向我解释如何开始构建它,我不想为此使用 bootstrap3(我已经看到 overlay 示例),而是想要纯 html5、css3、javascript/jquery)

最佳答案

您可以使用纯 HTML、CSS 和 JavaScript 创建弹出窗口,如 popup example 中所示.您想要的一个完整的工作示例在这里: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;
};

要使 map 项可点击,请使用

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;
  }
});

关于javascript - 使用 OpenLayer3 显示标记、弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34731134/

相关文章:

javascript - 如何将 AngularUI Google Map 对象传递给工厂或请求转换?

javascript - 我应该在 vb.net 上做什么来显示我的模式弹出窗口

java - Struts2 jquery 选项卡式面板

javascript - 如何在Chrome应用程序中打开base64 PDF?

javascript - 如果发生 Action 则增加变量

javascript - css 溢出无法正常工作

php - IE9 CSS3下拉菜单,错误错误错误

javascript - iframe 内的假点击不起作用

javascript - index.html 中始终禁用按钮标记

javascript - 基于 Href 值突出显示 Href 链接中匹配 ID 的行