Openlayers 3 如何使用字符串变量添加 kml?
Openlayers 3 how to add kml using a string variable?
有没有办法像这样在 openlayer 3 中添加 kml 向量,但 kml 来自字符串变量?
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml',
format: new ol.format.KML()
})
});
我试过这段代码,但它不起作用。
var kmlString = result;
var features = new ol.format.KML().readFeatures(kmlString);
var KMLvectorSource = new ol.source.Vector({
features: features
});
var KMLvector = new ol.layer.Vector({ source: KMLvectorSource });
//KMLvector.addFeatures(features);
map.addLayer(KMLvector);
提前致谢。
您缺少 kml 要素的投影定义和变换。
https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml
中存在的 kml 要素投影在 EPSG:4326
中,而您的地图投影在 EPSG:3857
中。因为 kml 通常会在 4326
中投影,所以在大多数情况下,kml 的 EPSG 代码应该是 4326。
现在回到你的问题。更改此行:
var features = new ol.format.KML().readFeatures(kmlString);
为此:
var features = new ol.format.KML().readFeatures(kmlString,{
dataProjection:'EPSG:4326',
featureProjection:'EPSG:3857'
});
有没有办法像这样在 openlayer 3 中添加 kml 向量,但 kml 来自字符串变量?
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: 'https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml',
format: new ol.format.KML()
})
});
我试过这段代码,但它不起作用。
var kmlString = result;
var features = new ol.format.KML().readFeatures(kmlString);
var KMLvectorSource = new ol.source.Vector({
features: features
});
var KMLvector = new ol.layer.Vector({ source: KMLvectorSource });
//KMLvector.addFeatures(features);
map.addLayer(KMLvector);
提前致谢。
您缺少 kml 要素的投影定义和变换。
https://openlayers.org/en/v4.2.0/examples/data/kml/2012-02-10.kml
中存在的 kml 要素投影在 EPSG:4326
中,而您的地图投影在 EPSG:3857
中。因为 kml 通常会在 4326
中投影,所以在大多数情况下,kml 的 EPSG 代码应该是 4326。
现在回到你的问题。更改此行:
var features = new ol.format.KML().readFeatures(kmlString);
为此:
var features = new ol.format.KML().readFeatures(kmlString,{
dataProjection:'EPSG:4326',
featureProjection:'EPSG:3857'
});