用于本地生成的geoJSON格式化对象的OpenLayers协议

时间:2014-10-30 17:28:02

标签: javascript jquery openlayers geojson

我正在使用我们的本地服务器生成地图,但我得到的形状数据只是数组。我使用JavaScript将其转换为geoJSON但是如何告诉OpenLayers使用此obj而不是URL?文档很不稳定,谷歌也没有给我答案。

我目前正在致电:

var marketProtocal = new OpenLayers.Protocol.HTTP({
   url: 'json/lte_shapes.json',
   format: new OpenLayers.Format.GeoJSON()
});

,容器看起来类似于:

var geoObj = {};

$.getJSON("/shape_server.json", function(result) {
     // convert arrays into new geoJSON obj
     geoObj.push(newGeoJSON);
   }).done(function() {
    // generate map using geoObj
});

1 个答案:

答案 0 :(得分:0)

以下代码对我有用:

<script type="text/javascript">
var vectorSource = new ol.source.ServerVector({
   format: new ol.format.GeoJSON(),
   loader: function(extent, resolution, projection) {
      loadFeatures();
   },
   strategy:  function(extent, resolution) {
   // some code
    return [extent];
   },
   projection: "EPSG:25832"
});

var localdata ={
  "type": "FeatureCollection",
  "crs": {"type": "name","properties": {"name": "EPSG:25832" }},
  "features": [
      { 
        "type": "Feature",
        "properties": {"name": "Hub 1"},
        "geometry": {"type": "Point","coordinates":  [714844,6178000]}
      }
    ]
  }

var loadFeatures = function() { 
   vectorSource.addFeatures(vectorSource.readFeatures(localdata));
};

var SmallworldLayer = new ol.layer.Vector({
   source: vectorSource,
   style: avlFeatureStyle
});
</script>

本地数据是我的GeoJson数据。用你自己的。