[英]OpenStreetMap and OpenLayer: simple marker labels
I'm trying to use an OpenStreetMap with OpenLayers.js, because I have to integrate in a web page a map with markers and labels of them. 我正在尝试将OpenStreetMap与OpenLayers.js一起使用,因为我必须在网页中集成带有标记和标签的地图。 I've searched everywhere, but I didn't find anything... I've this script:
我到处搜索过,但是什么也没找到...我有这个脚本:
// Definisco la variabilie mappa come un oggetto OpenLayers.Map utilizzando il DivMappa, poi aggiungo il Layer OSM (Open Street Map)
var Mappa = new OpenLayers.Map("DivMappa");
Mappa.addLayer(new OpenLayers.Layer.OSM());
// Creo l'oggetto contenente le coordinate (prima longitudine e poi latitudine)
var LonLat = new OpenLayers.LonLat( 12.492347, 41.890183 );
var LonLat2 = new OpenLayers.LonLat( 12.492347, 12.492347 );
// Imposto lo zoom
var zoom=16;
// Creo una variabile contenete il layer dei marker poi collego il layer dei markers alla mappa
var LayerMarkers = new OpenLayers.Layer.Markers( "Markers" );
Mappa.addLayer(LayerMarkers);
// Aggiungo al layer dei marker un marker (utilizzando l'oggetto lonLat per le coordinate)
LayerMarkers.addMarker(new OpenLayers.Marker(LonLat));
LayerMarkers.addMarker(new OpenLayers.Marker(LonLat2));
// Imposto le coordinate di lonLat come centro della mappa di partenza
Mappa.setCenter (LonLat, zoom);
With this I am only capable of creating the markers, but I need to show on the top of them a text of what position are displaying. 这样,我只能创建标记,但是我需要在标记的顶部显示正在显示什么位置的文本。 Is possible to add simple labels/title directly on markers?
可以直接在标记上添加简单的标签/标题吗? Like happens on GoogleMaps?
喜欢发生在GoogleMaps上吗? Thanks in advance!
提前致谢!
Makers are just images. 制作者只是图像。 They don't support text or labels.
他们不支持文字或标签。
I'd suggest you to use a popup instead, like this http://dev.openlayers.org/examples/osm-marker-popup.html 我建议您改用弹出式窗口,例如http://dev.openlayers.org/examples/osm-marker-popup.html
Otherwise, you should use a Layer.Vector with a StyleMap, like this http://dev.openlayers.org/examples/vector-features-with-text.html 否则,您应该将Layer.Vector与StyleMap一起使用,例如http://dev.openlayers.org/examples/vector-features-with-text.html
You can use ol.overlays to create markers with svg and then place them on your openlayers map like so: 您可以使用ol.overlays使用svg创建标记,然后将其放置在openlayers地图上,如下所示:
<svg id="popup" width="100" height="100">
<circle cx="50" cy="50" r="40"
stroke="green" stroke-width="4" fill="yellow" />
Sorry, your browser does not support inline SVG.
</svg>
<script type="text/javascript">
var popup = new ol.Overlay({
element: document.getElementById('popup')
});
popup.setPosition(ol.proj.fromLonLat([133.7751, -23.2744]));
map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'mainMap',
controls: ol.control.defaults({
attributionOptions: {
collapsible: false
}
}),
view: new ol.View({
center: ol.proj.fromLonLat([133.7751, -23.2744]),
zoom: 4
})
});
map.addOverlay(popup);
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.