如何将GeoJSON引入OpenLayers?
我已经尝试在代码中加入以下函数和新的向量层。我将GeoJSON文件上传到我的BPlaced帐户中,以便在代码中链接该文件,对吗? Geojson具有与网站相同的坐标系。代码似乎也能工作,但我没有看到任何Geojson 或者有没有其他方法将GeoJSON嵌入OpenLayers 这是我的密码:如何将GeoJSON引入OpenLayers?,openlayers,geojson,Openlayers,Geojson,我已经尝试在代码中加入以下函数和新的向量层。我将GeoJSON文件上传到我的BPlaced帐户中,以便在代码中链接该文件,对吗? Geojson具有与网站相同的坐标系。代码似乎也能工作,但我没有看到任何Geojson 或者有没有其他方法将GeoJSON嵌入OpenLayers 这是我的密码: var vectorLayerJSON=new ol.layer.Vector({ 来源:新ol.source.Vector({ 格式:新建ol.format.GeoJSON(), 网址:'http://k
var vectorLayerJSON=new ol.layer.Vector({
来源:新ol.source.Vector({
格式:新建ol.format.GeoJSON(),
网址:'http://kristinab.bplaced.net/ALDI_LIDL_Buffer_KBS_3857.geojson'
}),
风格:新的ol风格({
图片:新ol.style.Circle(({
半径:20,
填充:新的ol.style.fill({
颜色:“#ffff00”
})
}))
})
});
欢迎来到SO:)
我相信有几种方法可以将矢量(geojson)数据添加到地图中
1) 使用geojson文件url加载向量:
var vectorLayerJSON_1 = new ol.source.Vector({
projection : 'EPSG:3857',
url: 'myFolder/yourFile_1.geojson',
format: new ol.format.GeoJSON()
});
2) 从geojson对象生成矢量层
var geojsonObject = {
'type': 'FeatureCollection',
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:3857'
}
},
'features': [{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [0, 0]
}
}, {
'type': 'Feature',
'geometry': {
'type': 'LineString',
'coordinates': [[456, -256], [816, 226]]
}...
var vectorLayerJSON_2 = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
});
OpenLayer 3示例页面上更详细的示例
3) 从ajax读取矢量特性
var vectorLayerJSON_3 = new ol.layer.Vector({
renderMode: 'image',
source: new ol.source.Vector({
loader: function() {
$.ajax({
type: 'GET',
url: 'myFolder/yourFile_2.geojson',
context: this
}).done(function(data) {
var format = new ol.format.GeoJSON();
this.addFeatures(format.readFeatures(data));
});
}
}),
style: myDefinedStyle
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayerJSON_1,
vectorLayerJSON_2,
vectorLayerJSON_3
],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
希望有帮助:)