【问题标题】:How to convert Vector Layer coordinates into Map Latitude and Longitude in Openlayers如何在 Openlayers 中将矢量图层坐标转换为地图经纬度
【发布时间】:2010-04-08 16:37:41
【问题描述】:

我很困惑。我有一点:

x= -12669114.702301
y= 5561132.6760608

我使用 DrawFeature 控制器在矢量图层上绘制一个正方形。

这些数字看起来...呃...非常大,但它们似乎有效,因为如果我稍后绘制一个具有所有相同点的正方形,它位于相同的位置,所以我认为它们必须是正确的。

问题是当我尝试将此点转换为纬度和经度时。

我正在使用:

map.getLonLatFromPixel(pointToPixel(points[0]));

其中 points[0] 是几何点,pointToPixel 函数将任意点转换为像素(因为 getLonLatFromPixel 需要一个像素)。它通过简单地获取点的 x 并将其设为像素 x 来实现这一点,依此类推。

我得到的经纬度是:

lat: -54402718463.864
lng: -18771380.353223

这显然是错误的。我真的很困惑。我尝试投影这个对象,使用:

.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());

但我并没有真正理解它,而且我很确定我做错了,无论如何。

我的代码在这里:http://pastie.org/909644

我有点不知所措。坐标似乎是一致的,因为我可以重复使用它们来获得相同的结果......但它们似乎比我在 openLayers 网站上看到的任何示例都要大......

【问题讨论】:

    标签: openlayers latitude-longitude proj4js


    【解决方案1】:

    根据您的代码,您使用的投影是 EPSG:900913,这是 Google 使用的投影。此投影的单位是米,您为该点获得的值完全正确:

    x= -12669114.702301 (longitude)
    y= 5561132.6760608 (latitude)
    

    这些值不是像素,而是 EPSG:900913 投影中的坐标,并且是正确的(只要它们应该在 Idaho 中,否则其他地方有问题)

    要检查它,您可以转到 http://proj4js.org/ 并将您的坐标从 EPSG:900913 转换为 WGS84(纬度/经度),这将为您提供:

    x = -113.8085937334033 (longitude)
    y = 44.615123313472 (latitude)
    

    这是您可能期望的值。如果您想从点坐标中获取它们,请使用类似:

    point.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
    

    这会将坐标从 Google 投影转换为 WGS84(纬度/经度)。

    【讨论】:

    • 谢谢,我真的很难理解各种投影......
    • 其实我有个问题……我会再正式问一遍,但是:如果我反其道而行之(point.transform(new OpenLayers.Projection("EPSG:4326"), new OpenLayers. Projection("EPSG:900913")); 对于常规的纬度/经度点,对于任何负 lon 值,我得到 NaN(不是数字?)...这是预期的吗?
    • 这段代码给我带来了一些错误,弹出窗口没有在标记上打开。相反,这非常有效:OpenLayers.Layer.SphericalMercator.inverseMercator(lonlat.lon, lonlat.lat);
    【解决方案2】:

    据我所知,盒子处理程序的实现方式与 OL 中的其他处理程序不同。我们必须实现一个自己的处理程序,它返回一个带有 lon/lat 坐标而不是像素坐标的几何:

    Legato.Handler.Box = OpenLayers.Class(OpenLayers.Handler.Box, {
      endBox : function(end) {
        var result;
        if (Math.abs(this.dragHandler.start.x - end.x) > 5
            || Math.abs(this.dragHandler.start.y - end.y) > 5) {
          var start = this.dragHandler.start;
          var top = Math.min(start.y, end.y);
          var bottom = Math.max(start.y, end.y);
          var left = Math.min(start.x, end.x);
          var right = Math.max(start.x, end.x);
    
          var lowerLeftLonLat = this.map.getLonLatFromPixel(new OpenLayers.Pixel(
              left, bottom));
          var upperRightLonLat = this.map.getLonLatFromPixel(new OpenLayers.Pixel(
              right, top));
          var bounds = new OpenLayers.Bounds(lowerLeftLonLat.lon,
              lowerLeftLonLat.lat, upperRightLonLat.lon, upperRightLonLat.lat);
          result = bounds.toGeometry();
        } else {
          var xy = this.dragHandler.start.clone();
          var lonLat = this.map.getLonLatFromPixel(xy);
          result = new OpenLayers.Geometry.Point(lonLat.lon, lonLat.lat);
        }
        this.removeBox();
        this.callback("done", [ result ]);
      },
    
      CLASS_NAME :"Legato.Handler.Box"
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-09
      相关资源
      最近更新 更多