【问题标题】:Openlayers 3.6: Getting the center of the current map viewOpenlayers 3.6:获取当前地图视图的中心
【发布时间】:2015-07-10 19:09:57
【问题描述】:

我正在尝试以纬度/经度坐标获取 Openlayers 地图的当前中心。我有以下事件处理程序设置:

this.map.on('pointermove', function (e) {
    if (e.dragging) {
        console.log(that.map.getView().getCenter());
    }
});

这行得通,但我得到了奇怪的值。这是一个例子:

[9318218.659044644, 3274618.6225819485]

这些显然不是纬度/经度值 :) 我需要以某种方式对其进行转换吗?谢谢!

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    我对 openlayers 不是很熟悉,但听起来地图在不同的投影中。

    spherical_mercatortransform 上查看以下内容, Open Layers projections

    更新
    我做了更多的研究,看看这个example。不确定您的视图在什么投影中。如果您进入 js 控制台并输入 map.getView().getCenter(),则此示例中的地图位于“EPSG:21781”中,您将获得 [693230.7161150641, 179010.3389264635],但如果您输入 ol.proj.transform(map.getView().getCenter(), 'EPSG:21781', 'EPSG:4326'),您将获得 @ 987654328@希望有帮助。

    【讨论】:

    • 噢!我的地图在 EPSG:3857 中。以下行对我有用:ol.proj.transform(map.getView().getCenter(), 'EPSG:3857', 'EPSG:4326') 谢谢!
    • OpenLayers 看起来很酷。我只用过谷歌地图,但我可能不得不玩。
    【解决方案2】:

    OpenLayers 6 的更新(假设您的地图称为“地图”)以下为地图视图的中心提供了一个 [lon, lat] 数组。

    ol.proj.toLonLat( map.getView().getCenter() )
    

    【讨论】:

      【解决方案3】:

      对于使用 angular 的人。
      另外,我会考虑搜索地图的投影,而不是手动输入。

      import * as olProj from 'ol/proj'
      import Map from 'ol/Map'
      
      // ...
      
      olProj.transform(
          this.map.getView().getCenter(),
          this.map.getView().getProjection(),
          'EPSG:4326',
      )
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-23
        • 1970-01-01
        • 1970-01-01
        • 2014-04-12
        • 1970-01-01
        相关资源
        最近更新 更多