简体   繁体   English

带弹出窗口的openlayers标记

[英]openlayers markers with popup

I am trying to display a map with markers. 我正在尝试显示带有标记的地图。 I want the ability to click these markers such that extra information can be displayed (similiar to the way it works in google earth). 我希望能够单击这些标记,以便显示更多信息(类似于它在Google Earth中的工作方式)。 I have the map and the markers (or features) but can not get the "popup" with extra information to work. 我有地图和标记(或特征),但无法获得带有额外信息的“弹出窗口”以供工作。

The JS: JS:

function init(){
    var northSeaLonLat = [4.25, 52.05];
    var centerWebMercator = ol.proj.fromLonLat(northSeaLonLat);

    var tileLayer = new ol.layer.Tile({ source: new ol.source.OSM() });
    markerLayer = new ol.layer.Vector({ source: new ol.source.Vector({ features: [], projection: 'EPSG:3857' }) });


    var map = new ol.Map({
        controls: ol.control.defaults().extend([
            new ol.control.MousePosition({
                coordinateFormat: ol.coordinate.createStringXY(3),
                projection: 'EPSG:4326',
                undefinedHTML: ' ',
                className: 'custom-mouse-position',
                target: document.getElementById('custom-mouse-position'),
            })
        ]),
        layers: [tileLayer, markerLayer],
        target: 'map',
        view: new ol.View({
            projection: 'EPSG:3857',
            center: centerWebMercator,
            zoom: 7
        })
    });

    // Add marker
    var circle = new ol.style.Style({
        image: new ol.style.Circle({
            radius: 4,
            fill: new ol.style.Fill({
                color: 'rgba(200,0,0,0.9)',
            }),
            stroke: new ol.style.Stroke({
                color: 'rgba(100,0,0,0.9)',
                width: 2
            })
        })
    });

    coordinates = [[4.25, 52.05], [4.21, 52.01], [4.29, 52.29], [5.25, 52.05], [4.25, 51.05]];
    for (i = 0; i < coordinates.length; i++) { 
        var feature = new ol.Feature(
            new ol.geom.Point(ol.proj.transform(coordinates[i], 'EPSG:4326', 'EPSG:3857'))
        );
        feature.description = 'Coordinates: '+coordinates[i][0]+','+coordinates[i][1]+'\nBla';
        feature.setStyle(circle);
        markerLayer.getSource().addFeature(feature);
    }

    var element = document.getElementById('popup');
    var popup = new ol.Overlay({
      element: element,
      positioning: 'bottom-center',
      stopEvent: false
    });
    map.addOverlay(popup);

    // display popup on click
    map.on('click', function(evt) {
        var feature = map.forEachFeatureAtPixel(evt.pixel,
            function(feature, layer) {
                return feature;
            });
        if (feature) {
            popup.setPosition(evt.coordinate);
            $(element).popover({
                'placement': 'top',
                'html': true,
                'content': feature.get('description')
            });
            $(element).popover('show');
        } else {
            $(element).popover('destroy');
        }
    });
}

The code I got from an example on the website: http://openlayers.org/en/v3.11.1/examples/icon.html 我从网站上的示例获取的代码: http : //openlayers.org/en/v3.11.1/examples/icon.html

It works there but I can't get my version to work. 它在那里工作,但是我无法使用我的版本。 Any idea why? 知道为什么吗?

popover isn't part of OpenLayers but contained in Bootstrap: http://getbootstrap.com/javascript/#popovers popover不是OpenLayers的一部分,而是包含在Bootstrap中: http : //getbootstrap.com/javascript/#popovers

Also see the OpenLayers example on overlays: https://openlayers.org/en/latest/examples/overlay.html 另请参见叠加层上的OpenLayers示例: https : //openlayers.org/en/latest/examples/overlay.html

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

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