【发布时间】:2015-08-10 14:55:10
【问题描述】:
目前我有一个使用 KML 文件作为矢量源显示的矢量地图。
我想要做的是在这张矢量图上放置一张图片。
该地图是室内平面图,图像与矢量图完全相同,只是其中有更多细节,上面写有文字等。我需要的是地图图像在矢量图下方,以便矢量地图的墙壁与图像的墙壁完美对齐。之所以会发生这种情况,是因为 KML 是通过使用 QGIS 在图像之上进行跟踪而创建的。
到目前为止,我已经能够让 KML 矢量地图和 png 图像出现在地图上,但是它们彼此不对齐并且大小不一样。这就是我需要帮助的地方!
这里有一些我目前拥有的代码:
创建地图,还没有图层(从下拉框中选择地图)
var map = new ol.Map({
layers: [],
target: 'floormap',
interactions: ol.interaction.defaults({mouseWheelZoom:false}),
view: new ol.View({
center: [0, 0],
zoom: 19,
minZoom: 15,
maxZoom: 30
})
});
将所选地图 (KML) 添加到地图
map.removeLayer(vector);
vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: MAPS_URL + maps[map_id],
format: new ol.format.KML()
})
});
map.addLayer(vector);
setMapExtent(vector);
现在我尝试添加图像,该图像有效但未对齐
// this part here i feel may be the problem,
// i just copied and pasted from an example om openlayers.org,
// i dont actually know much about the extent and how to match it to
// the vector map
var extent = [0,0,1024,684];
var projection = new ol.proj.Projection({
code: 'xkcd-image',
units: 'pixels',
extent: extent
});
image = new ol.layer.Image({
source: new ol.source.ImageStatic({
attributions: [
new ol.Attribution({
html: '© <a href="http://xkcd.com/license.html">xkcd</a>'
})
],
url: MAPS_URL + images[map_id],
projection: projection,
imageExtent: extent
})
});
map.addLayer(image);
setMapExtent 方法
function setMapExtent(vectorMap) {
var vectorSource = vectorMap.getSource();
var listenerKey = vectorSource.on('change', function () {
if (vectorSource.getState() === 'ready') {
var extent = vectorSource.getExtent();
map.getView().fitExtent(extent, map.getSize());
vectorSource.unByKey(listenerKey);
}
});
}
此时,我有一张矢量图,其图像位于地图上方,而且图像似乎也更小。
谁能帮我解决这个问题?
*** 解决方案! ***
一个可行的解决方案,虽然可能不是最好的方法,但它仍然有效。
var map = new ol.Map({
layers: [],
target: 'floormap',
interactions: ol.interaction.defaults({mouseWheelZoom:false}),
view: new ol.View({
center: [0, 0],
zoom: 19,
minZoom: 15,
maxZoom: 30
})
});
添加新的地图层
map.removeLayer(vector);
vector = new ol.layer.Vector({
source: new ol.source.Vector({
url: MAPS_URL + maps[map_id],
format: new ol.format.KML()
})
});
map.addLayer(vector);
setMapExtent(vector);
// call image adding function pass in vector
// to get its extend
addImage(vector);
addImage 函数
function addImage(vectorMap) {
var vectorSource = vectorMap.getSource();
// listen for one change on the vector to get the extent of it
// for use in setting the image extent. tried to use on.('load')
// but it didnt work
var listenerKey = vectorSource.once('change', function () {
var extent = vectorSource.getExtent();
var projection = new ol.proj.Projection({
code: 'xkcd-image',
units: 'pixels',
extent: extent
});
image = new ol.layer.Image({
source: new ol.source.ImageStatic({
attributions: [],
url: MAPS_URL + images[map_id],
projection: projection,
imageExtent: extent
})
});
// remove vector layer else they keep stacking up
map.removeLayer(vector);
// add image
map.addLayer(image);
// re-add vector only push so it goes above the image
map.getLayers().push(vector);
});
}
似乎工作得很好!任何人都可以帮助我进行图层排序吗?
【问题讨论】:
标签: javascript vector maps kml openlayers-3