2015-09-25 83 views
0

我已經通過了幾個不同的例子,並且似乎沒有任何工作。我試圖用GeoJSON作爲源在地圖上繪製一個點。這是我目前有的:將geojson圖層添加到開放圖層3

var staticGeo = new ol.source.GeoJSON(({ 
    object: { 
     type: 'Feature Collection', 
     crs: { 
      type: 'name', 
      properties: {name: 'EPSG:4326'} 
     }, 
     features: [{ 
      type: 'Feature', 
      geometry: { 
       type: 'Point', 
       coordinates: [0,0] 
      } 
     }] 
    }, 
    projection: 'EPSG:3857' 
    })); 

    var vectorSource = new ol.source.Vector({ 
     source: staticGeo 
    }); 

    var vectorLayer = new ol.layer.Vector({ 
     source: vectorSource, 
     style: new ol.style.Style({ 
      fill: new ol.style.Fill({ 
       color: 'rgba(255,255,255,0.2)' 
      }), 
      stroke: new ol.style.Stroke({ 
       color: 'blue', 
       width: 1 
      }) 
     }) 
    }) 

    this.map.addLayer(vectorLayer); 

this.map引用正在工作的ol.Map對象。這整體看起來像很多代碼做一些應該看似微不足道(也許我做錯了什麼?)。

+0

您能夠使用最新的OL 3版本嗎? –

+0

使用OL 3.4.0,如果我使用3.9.0,此代碼是否工作? @JonatasWalker – user1200387

回答

1

OL 3.5.0您將添加geojson這樣的:

var geojsonObject = { 
    'type': 'FeatureCollection', 
    'crs': { 
     'type': 'name', 
     'properties': { 
      'name': 'EPSG:4326' 
     } 
    }, 
    'features': [ 
     { 
      'type': 'Feature', 
      'geometry': { 
       'type': 'Point', 
       'coordinates': [21.54967, 38.70250] 
      } 
     } 
    ] 
}; 
var features = new ol.format.GeoJSON().readFeatures(geojsonObject, { 
    featureProjection: 'EPSG:3857' 
}); 
var vectorSource = new ol.source.Vector({ 
    features: features 
}); 

注意投影座標。

http://jsfiddle.net/jonataswalker/w5uuxhov/

+0

這對3.4仍然不起作用,但我會升級到3.9並希望最好。謝謝你的幫助。 – user1200387

+0

3.4和更低版本不適用。別客氣。 –

相關問題