从 API 到矢量图层 openlayers JSON
From API to vector layer openlayers JSON
如果要将系统中的 json 文件导入矢量图层,非常简单:
var restaurantsold = new ol.layer.Vector({
title: 'b_layer',
source: new ol.source.Vector({
url: 'restaurantjson.geojson',
format: new ol.format.GeoJSON()
}),
});
而且我可以将该图层直接添加到地图中。但是如果我尝试调用 API,我无法在地图中显示它,我最好的尝试是这样的:
var restaurants;
$.ajax({
type: "GET",
url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita",
dataType:"json",
success:function(data){
console.log(data)
restaurants = data;
$(restaurants).each(function(index, value) {
console.log(value.address);
});
}
});
var resta2 = new ol.layer.Vector({
title : "Resta2",
source: new ol.source.Vector(restaurants)
});
而且我在任何地方都找不到合适的解决方案,感谢您的帮助!
编辑:
最后的问题是它得到一个 JSON 文件,而 openlayers 想要一个 GeoJSON 文件。我解决它的方法是将它转换为 GeoJSON 如下:
https://gis.stackexchange.com/questions/73756/is-it-possible-to-convert-regular-json-to-geojson
创建矢量图层时,餐厅数据可能根本不可用,因为您正在进行 Ajax 通话。
因此,使用 ol.format.GeoJSON
readFeatures()
方法将 GeoJSON 转换为 ol.Feature
对象的集合。然后使用addFeatures()
方法添加矢量源。
修复:
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON()
})
var restaurantsold = new ol.layer.Vector({
title: 'b_layer',
source : vectorSource
});
$.ajax({
type: "GET",
url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita",
dataType:"json",
success:function(data){
// If response is valid
var geojsonFormat = new ol.format.GeoJSON();
// reads and converts GeoJSon to Feature Object
var features = geojsonFormat.readFeatures(data);
vectorSource.addFeatures(features);
}
});
如果要将系统中的 json 文件导入矢量图层,非常简单:
var restaurantsold = new ol.layer.Vector({
title: 'b_layer',
source: new ol.source.Vector({
url: 'restaurantjson.geojson',
format: new ol.format.GeoJSON()
}),
});
而且我可以将该图层直接添加到地图中。但是如果我尝试调用 API,我无法在地图中显示它,我最好的尝试是这样的:
var restaurants;
$.ajax({
type: "GET",
url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita",
dataType:"json",
success:function(data){
console.log(data)
restaurants = data;
$(restaurants).each(function(index, value) {
console.log(value.address);
});
}
});
var resta2 = new ol.layer.Vector({
title : "Resta2",
source: new ol.source.Vector(restaurants)
});
而且我在任何地方都找不到合适的解决方案,感谢您的帮助!
编辑: 最后的问题是它得到一个 JSON 文件,而 openlayers 想要一个 GeoJSON 文件。我解决它的方法是将它转换为 GeoJSON 如下: https://gis.stackexchange.com/questions/73756/is-it-possible-to-convert-regular-json-to-geojson
创建矢量图层时,餐厅数据可能根本不可用,因为您正在进行 Ajax 通话。
因此,使用 ol.format.GeoJSON
readFeatures()
方法将 GeoJSON 转换为 ol.Feature
对象的集合。然后使用addFeatures()
方法添加矢量源。
修复:
var vectorSource = new ol.source.Vector({
format: new ol.format.GeoJSON()
})
var restaurantsold = new ol.layer.Vector({
title: 'b_layer',
source : vectorSource
});
$.ajax({
type: "GET",
url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita",
dataType:"json",
success:function(data){
// If response is valid
var geojsonFormat = new ol.format.GeoJSON();
// reads and converts GeoJSon to Feature Object
var features = geojsonFormat.readFeatures(data);
vectorSource.addFeatures(features);
}
});