2014-10-30 81 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數據。用你自己的。