我试图了解如何使用openlayers3在osm地图上显示标记/弹出窗口,我已经在ol3网页上找到了示例,但是.
是否有更多使用javascript或jquery编写标记/弹出窗口的示例,最好是类似于这或类似示例的示例。
这个想法是为了标记一座建筑,点击标记它会弹出一些建筑的信息,而且我还想把重要的地方从城市连接到这座建筑(图书馆,餐馆,公交车站等等)。我希望有人能解释我如何开始构建这个程序,我不想为此使用bootstrap3 (我看过覆盖示例),而是想要纯html5、css3、javascript/jquery)
发布于 2016-01-13 14:34:00
您可以使用纯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;
}
});https://stackoverflow.com/questions/34731134
复制相似问题