Openlayers 3.6:获取当前地图视图的中心
Openlayers 3.6: Getting the center of the current map view
我正在尝试获取 lat/lon 坐标中 Openlayers 地图的当前中心。我有以下事件处理程序设置:
this.map.on('pointermove', function (e) {
if (e.dragging) {
console.log(that.map.getView().getCenter());
}
});
这行得通,但我得到了奇怪的值。这是一个例子:
[9318218.659044644, 3274618.6225819485]
这些显然不是 lat/lon 值 :) 我需要以某种方式对其进行转换吗?谢谢!
我对 openlayers 不是很熟悉,但听起来地图是不同的投影。
查看 spherical_mercator, transform 上的以下内容,
Open Layers projections
已更新
我做了更多的研究,看看这个 example。不确定你的视图是什么投影。这个例子中的地图在''EPSG:21781'如果你去一个js控制台并输入map.getView().getCenter()
你得到[693230.7161150641, 179010.3389264635]
但如果你输入ol.proj.transform(map.getView().getCenter(), 'EPSG:21781', 'EPSG:4326')
你得到 [8.658936030357363, 46.75575224283748]
希望对你有所帮助。
OpenLayers 6 更新(假设您的地图名为 'map')以下给出了地图视图中心的 [lon, lat] 数组。
ol.proj.toLonLat( map.getView().getCenter() )
对于那些与 angular 一起工作的人。
另外,我会考虑搜索地图的投影,而不是手动输入。
import * as olProj from 'ol/proj'
import Map from 'ol/Map'
// ...
olProj.transform(
this.map.getView().getCenter(),
this.map.getView().getProjection(),
'EPSG:4326',
)
我正在尝试获取 lat/lon 坐标中 Openlayers 地图的当前中心。我有以下事件处理程序设置:
this.map.on('pointermove', function (e) {
if (e.dragging) {
console.log(that.map.getView().getCenter());
}
});
这行得通,但我得到了奇怪的值。这是一个例子:
[9318218.659044644, 3274618.6225819485]
这些显然不是 lat/lon 值 :) 我需要以某种方式对其进行转换吗?谢谢!
我对 openlayers 不是很熟悉,但听起来地图是不同的投影。
查看 spherical_mercator, transform 上的以下内容, Open Layers projections
已更新
我做了更多的研究,看看这个 example。不确定你的视图是什么投影。这个例子中的地图在''EPSG:21781'如果你去一个js控制台并输入map.getView().getCenter()
你得到[693230.7161150641, 179010.3389264635]
但如果你输入ol.proj.transform(map.getView().getCenter(), 'EPSG:21781', 'EPSG:4326')
你得到 [8.658936030357363, 46.75575224283748]
希望对你有所帮助。
OpenLayers 6 更新(假设您的地图名为 'map')以下给出了地图视图中心的 [lon, lat] 数组。
ol.proj.toLonLat( map.getView().getCenter() )
对于那些与 angular 一起工作的人。
另外,我会考虑搜索地图的投影,而不是手动输入。
import * as olProj from 'ol/proj'
import Map from 'ol/Map'
// ...
olProj.transform(
this.map.getView().getCenter(),
this.map.getView().getProjection(),
'EPSG:4326',
)