2014-10-30 47 views
0

我使用本地服务器生成地图,但我得到的形状数据只是数组。我正在使用JavaScript将其转换为geoJSON,但我如何告诉OpenLayers使用此obj而不是URL?文档很薄,Google没有给我答案。本地生成的geoJSON格式化对象的OpenLayers协议

什么我正在打电话:

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 
}); 

回答

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数据。用你自己的。