2015-09-23 65 views
0

一個幾何特性在OL3,有以GeoJSON與性能的一個叫geometry失敗:以GeoJSON隨着OL3

var geojsonObject = { 
    "type": "FeatureCollection", 
    "features": [{ 
     "type": "Feature", 
     "geometry": { 
      "type": "Point", 
      "coordinates": [102.0, 0.5] 
     }, 
     "properties": { 
      "prop0": "value0", 
      "geometry": "This is a point" 
     } 
    }] 
}; 

features = new ol.format.GeoJSON().readFeatures(geojsonObject); 

console.log(features[0].getGeometry()); 
console.log(features[0].get('prop0')); 

這是合法的嗎?應該支持嗎?

回答

1

是的,具有名爲geometry的要素屬性是有效的GeoJSON。以GeoJSON說明書中明確指出的屬性構件的

值是一個對象(任何JSON對象或JSON空值)

的OpenLayers 3存儲的幾何形狀上的特徵的正常屬性,默認名稱爲geometry。在閱讀您的問題中的功能時,geometry GeoJSON功能成員首先被寫入geometry屬性,但之後被geometry GeoJSON屬性成員覆蓋。

爲了避免這種碰撞,必須使用不同的屬性名稱來存儲幾何圖形。您可以使用選項來控制ol.format.GeoJSON

var geoJSONFormat = new ol.format.GeoJSON({ 
    geometryName: 'actualGeometry' 
}); 
geoJSONFormat.readFeatures(geojsonObject);