我有一个名为 lat_longs
的数组,其中包含一些经纬度(长这样 - [[39.749318, -104.9701129], [..], [..]]
),我想在使用 Open Layers 3 的 OpenStreetMap 上将它们绘制出来。以下是我编写的代码 -
var icon_features = [];
$.each(lat_longs, function(index, item){
var point = new ol.geom.Point(item);
point.transform('EPSG:4326', 'EPSG:900913');
// I tried it the other way too, but doesn't seem to work
var iconFeature = new ol.Feature({
geometry: point,
name: item.name
});
icon_features.push(iconFeature);
});
var vectorSource = new ol.source.Vector({
features: icon_features
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var view = new ol.View({
center: [0,0],
zoom: 2
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
vectorLayer
],
target: 'map',
controls: ol.control.defaults({
attributionOptions: ({
collapsible: false
})
}),
view: view
});
由于某种原因,它似乎要么在非洲附近绘制位置,要么根本不绘制位置。
我该如何解决这个问题?
我找到了在Open Layers 2中进行投影和变换的代码。但是在Open Layers 3中不太清楚如何做。
注意:通过tsauerwein的评论,我已经解决了这个问题。但请注意,我必须将点从EPSG:4326转换为EPSG:900913。
[lat, lon]
还是[lon, lat]
?OL3希望它们是[lon, lat]
。 - tsauerwein