试图在传单中使用EPSG:3857 [英] trying to use EPSG:3857 in Leaflet

查看:616
本文介绍了试图在传单中使用EPSG:3857的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使传单使用EPSG:3857作为输入坐标系.我使用porj4leaflet来实现这一点.我已经这样定义了地图实例:

I am trying to make leaflet use EPSG:3857 as an input coordinate system. I use porj4leaflet to achieve this. I have defined my map instance like this:

var map = L.map('map', {
center: [8378860.13, 1481133.311008498],
zoom: 7,
crs: new L.Proj.CRS(
  'EPSG:3857',
  '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs',
    {
      resolutions: [
        8192, 4096, 2048, 1024, 512, 256, 128
      ],
      origin: [0, 0]
    }
  )
});

当我尝试运行此命令时,我得到了TypeError: coordinates must be finite numbers.您可以在此处看到整个小提琴: https://jsfiddle.net/asdpinspdai/fckbpq0a/

When I try to run this, i get TypeError: coordinates must be finite numbers. You can see the entire fiddle here: https://jsfiddle.net/asdpinspdai/fckbpq0a/

据我了解,从文档中了解到,像这样设置crs应该可以将坐标传递给EPSG:3857格式的传单.我想念什么吗?

Of what I understand from the docs, setting the crs like this should allow me to pass coordinates to leaflet in the EPSG:3857 format. Am I missing something?

这里的任何帮助都是非常非常有用的.谢谢

Any help here is very, very appriciated. Thank you

推荐答案

有限数字"错误是因为L.Map期望center坐标在-90到90度和-180到180度范围内.

The "finite numbers" error is because L.Map expects the center coordinates to be in the range -90 to 90 and -180 to 180 degrees.

在jsfiddle中,您还会遇到选择zoom:11的问题,但是CRS定义中只有7个缩放级别可用.

In your jsfiddle, you also have the problem that you are selecting zoom:11, but only have 7 zoom levels available in your CRS definition.

Leaflet API始终使用WGS84纬度/经度坐标(以度为单位),因此以这种方式使用Proj4leaflet将无法完成您想要的操作.请参见来自Proj4Leaflet项目的说明.

The Leaflet API always uses WGS84 latitude/longitude coordinates in degrees, so using Proj4leaflet this way won't do what you want. See this explanation from the Proj4Leaflet project.

但是,EPSG:3857已经是Leaflet中的默认CRS,您可以使用其内置方法在米和WGS84度之间进行转换,而无需Proj4Leaflet或Proj4js.请参阅传单CRS文档.

However, EPSG:3857 is already the default CRS in Leaflet, and you can use its built-in methods to convert between meters and WGS84 degrees, without needing either Proj4Leaflet or Proj4js. See the Leaflet CRS documentation.

const proj = L.CRS.EPSG3857;

// Degrees to metres
proj.project(new L.LatLng(51,-2));             // {x: -222638.98158654, y: 6621293.722740}

// Metres to degrees
proj.unproject(new L.Point(-222600, 6621000)); // {lat: 50.998339473, lng: -1.99964982245}

每当需要将仪表坐标传递到WGS时,将c <4>转换为WGS可能是最简单的,但是您也可以编写一些Leaflet功能的覆盖版本.这些将获取仪表坐标,将其转换为度,然后将其传递给原始函数.

It's probably easiest to unproject your metre coordinates to WGS whenever you need to pass them to Leaflet, but alternatively you could write overridden versions of some of Leaflet's functions. These would take metre coordinates, convert them to degrees, then pass them on the original function.

这篇关于试图在传单中使用EPSG:3857的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆