OpenLayers3如何使用不同的Geojson源显示来自不同矢量层的弹出窗口

时间:2018-09-04 10:21:19

标签: openlayers

尝试将我的地图从openlayers 2升级到OL3或更高版本。在我的地图上,我有几个具有不同Geojson源的矢量层。对于每个这些矢量层,我都需要一个弹出窗口来显示单击的特征的详细信息。 目前,我可以毫无问题地显示所有图层,但是当出现弹出窗口时,会出现一些问题。参见下面的代码:

var geoJSONFormat = new ol.format.GeoJSON();

var osmMap = new ol.layer.Tile({
  source: new ol.source.OSM({
    "url" : "https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png"
  })
});


var hotels = new ol.layer.Vector({
  id: 'hotels',
  source: new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url: 'hotels-geojson.php'
  }) 
});

var cwo = new ol.layer.Vector({
  id: 'cwo',
  source: new ol.source.Vector({
      format: new ol.format.GeoJSON(),
      url: 'source-of-cwo-in-geojson-format'
  }) 
});    

var element = document.getElementById('popup');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');    

var map = new ol.Map({
  layers: [osmMap, hotels, cwo],
  target: document.getElementById('map'),
  view: new ol.View({
    projection: 'EPSG:4326',
    center: [0,0],
    maxZoom: 19,
    zoom: 11
  })
});    

var popup = new ol.Overlay({
  element: element,
  positioning: 'bottom-left',
  stopEvent: false,
  offset: [0, 0]
});
map.addOverlay(popup);    

var hotelsfeature = null;
var cwofeature = null;   


 // display popup on click
    map.on('click', function(evt) {
  map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
    if (layer == hotels) {
      hotelsfeature = feature;
    }
   else if (layer == cwo){
      cwofeature = feature;

    }          

});

  if (hotelsfeature) {
    var geometry = hotelsfeature.getGeometry(); // access the geometry section of the Geojson
    var coord = geometry.getCoordinates(); // get the coordinates in the geometry section

    var element = '<h3>' + hotelsfeature.get('h_name') + '</h3>';        

    content.innerHTML = element;

     // add the content to the html
    popup.setPosition(coord); 

    var popWidth = $("#popup").outerWidth();            
    var popHeight = $("#popup").outerHeight();
    var mapWidth = $("#map").width();
    var mapHeight = $("#map").height();
    var distPopW = mapWidth - popWidth;
    var distPopH = mapHeight - popHeight;
    var pointPix =  map.getPixelFromCoordinate(coord);
    if (pointPix[0] > distPopW && pointPix[1] > distPopH) {
      $('#popup').removeClass();
      $('#popup').addClass('popleft');
      $('#popup').addClass('poptop');
      $('#popup').addClass('arrow-bottom-right');
    }
    else if (pointPix[0] > distPopW && pointPix[1] < distPopH ) {
      $('#popup').removeClass();
      $('#popup').addClass('popleft');
      $('#popup').addClass('popbottom');
      $('#popup').addClass('arrow-top-right');
    }
    else if (pointPix[0] < distPopW && pointPix[1] > distPopH ) {
      $('#popup').removeClass();
      $('#popup').addClass('poptop');
      $('#popup').addClass('popright');
      $('#popup').addClass('arrow-bottom-left');
    }
    else if (pointPix[0] < distPopW && pointPix[1] < distPopH  ) {
      $('#popup').removeClass();
      $('#popup').addClass('popbottom');
      $('#popup').addClass('popright');
      $('#popup').addClass('arrow-top-left');
    }
  }

  else if (cwofeature) {
    var geometry = cwofeature.getGeometry(); // access the geometry section of the Geojson
    var coord = geometry.getCoordinates(); // get the coordinates in the geometry section

    var element = '<h3>' + cwofeature.get('pla_short_name') + '</h3>';        

    content.innerHTML = element;

     // add the content to the html
    popup.setPosition(coord); 

    var popWidth = $("#popup").outerWidth();            
    var popHeight = $("#popup").outerHeight();
    var mapWidth = $("#map").width();
    var mapHeight = $("#map").height();
    var distPopW = mapWidth - popWidth;
    var distPopH = mapHeight - popHeight;
    var pointPix =  map.getPixelFromCoordinate(coord);
    if (pointPix[0] > distPopW && pointPix[1] > distPopH) {
      $('#popup').removeClass();
      $('#popup').addClass('popleft');
      $('#popup').addClass('poptop');
      $('#popup').addClass('arrow-bottom-right');
    }
    else if (pointPix[0] > distPopW && pointPix[1] < distPopH ) {
      $('#popup').removeClass();
      $('#popup').addClass('popleft');
      $('#popup').addClass('popbottom');
      $('#popup').addClass('arrow-top-right');
    }
    else if (pointPix[0] < distPopW && pointPix[1] > distPopH ) {
      $('#popup').removeClass();
      $('#popup').addClass('poptop');
      $('#popup').addClass('popright');
      $('#popup').addClass('arrow-bottom-left');
    }
    else if (pointPix[0] < distPopW && pointPix[1] < distPopH  ) {
      $('#popup').removeClass();
      $('#popup').addClass('popbottom');
      $('#popup').addClass('popright');
      $('#popup').addClass('arrow-top-left');
    }
  }    

});   


// change mouse cursor when over marker
map.on('pointermove', function(e) {
          if (e.dragging) return;
          var pixel = map.getEventPixel(e.originalEvent);
          var hit = map.hasFeatureAtPixel(pixel);

          map.getTarget().style.cursor = hit ? 'pointer' : '';
        });

使用此代码,当我单击“酒店”层的功能时,我无法再单击“ cwo”层。但是,如果我先单击“ cwo”功能,则弹出窗口将打开,然后我也可以打开“ hotels”功能,但无法返回“ cwo”层。 如果有人能指出正确的方向,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

经过数小时的尝试和移动,我终于使它工作了。 测试单击哪个层时,需要直接在条件中添加该层的弹出窗口创建。参见下面的代码:

    map.on('click', function(evt) {
        map.forEachFeatureAtPixel(evt.pixel, function(feature, layer) {
           if (layer === hotels) {
               hotelsfeature = feature;
               if (hotelsfeature) {
                    var geometry = hotelsfeature.getGeometry(); 
                    var coord = geometry.getCoordinates(); 
                    var element = '<h3>' + hotelsfeature.get('h_name') + '</h3>'; 

                    content.innerHTML = element;
                    popup.setPosition(coord);
               }
          }
          else if (layer === cwo){
              cwofeature = feature;
              if (cwofeature) {
                   var geometry = cwofeature.getGeometry(); 
                   var coord = geometry.getCoordinates(); 
                   var element = '<h3>' + cwofeature.get('pla_short_name') + '</h3>';  
                   content.innerHTML = element;
                   popup.setPosition(coord); 
              }
        }
      });
   });