How to convert Vector Layer coordinates into Map Latitude and Longitude in Openlayers

旧城冷巷雨未停 提交于 2019-11-28 20:27:43

According to your code, the projection you are using is EPSG:900913, which is the one that Google uses. The units for this projection are meters, and the values that you obtain for the point are perfectly correct:

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

This values are not pixels but coordinates in the EPSG:900913 projection, and are correct (as long as they are supposed to be in Idaho, if not there is something wrong elsewhere)

To check it, you can go to http://proj4js.org/ and transform your coordinates from EPSG:900913 to WGS84 (lat/lon), which will give you:

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

This are the values you are probably expecting. If you want to obtain them from the point coordinates use something like:

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

This will transform the coordinates from the Google projection to WGS84 (Latitude / Longitude).

As far as I remember, box handler is implementy differently from other handlers in OL. We had to implement an own handler which returns a geometry with lon/lat coordinates rather that with pixel coordinates:

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"
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!