How to add shapefiles to a Bing Map using Openlayers 3

戏子无情 提交于 2019-12-02 07:32:14

The example you linked is from a very old beta version of OpenLayers 3. You can find the examples from the last release here.

You are talking about shapefiles, but given the fact that you used OpenLayers.Layer.GML in OpenLayers 2 and have tagged this post with geoserver and gml as well, I assume you have uploaded your shapefile to GeoServer and use WFS to access the data.

So the relevant example to look at will be http://openlayers.org/en/v3.3.0/examples/vector-wfs.html. That example uses JSONP as transport. In your case, with a local GeoServer, your vector source definiton would be a bit simpler and could look like this:

var vectorSource = new ol.source.ServerVector({
  format: new ol.format.GeoJSON(),
  loader: function(extent, resolution, projection) {
    var url = 'geoserver/wfs?service=WFS&version=1.1.0&' +
        'request=GetFeature&typename=osm:water_areas&outputFormat=json' +
        '&srsname=EPSG:3857&bbox=' + extent.join(',') + ',EPSG:3857';
    $.ajax(url).then(function(response) {
      vectorSource.addFeatures(vectorSource.readFeatures(response));
    });
  },
  strategy: ol.loadingstrategy.createTile(new ol.tilegrid.XYZ({
    maxZoom: 19
  }))
});

In the above snippet, you will have to replace osm:water_areas with your workspace and layer (feature type) name.

To use that source in a vector layer, you can also add some style if you don't want the default blueish fill. The one below renders the features with just a blue outline that is 2 pixels wide:

var vector = new ol.layer.Vector({
  source: vectorSource,
  style: new ol.style.Style({
    stroke: new ol.style.Stroke({
      color: 'rgba(0, 0, 255, 1.0)',
      width: 2
    })
  })
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!