javascript - openlayers markers with popup -
i trying display map markers. want ability click these markers such information can displayed (similiar way works in google earth). have map , markers (or features) can not "popup" information work.
the 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]]; (i = 0; < 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 got example on website: http://openlayers.org/en/v3.11.1/examples/icon.html
it works there can't version work. idea why?
popover
isn't part of openlayers contained in bootstrap: http://getbootstrap.com/javascript/#popovers
also see openlayers example on overlays: https://openlayers.org/en/latest/examples/overlay.html
Comments
Post a Comment