2015-09-25 81 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和更低版本不适用。别客气。 –

相关问题