如何将Leaflet坐标切换到西南0,0? [英] How do switch Leaflet coordinates to where 0,0 is southwest?

查看:187
本文介绍了如何将Leaflet坐标切换到西南0,0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个传单项目中,我需要0到4096的坐标,而我需要的传单坐标如下:

I am working on a leaflet project where I need the coordinates 0 to 4096 and which the leaflet coordinates that I need are the following:

0,0 bottom left (southWest)
0,4096' top left (northWest)
4096',4096' top right (northEast)
4096',0 bottom right (southEast)

我已经尝试了很多方法来根据需要获取坐标,但是没有运气.这是我的 jsfiddle 和我的示例,当您单击显示以下内容的地图时,该示例具有console.log该坐标.如果单击左上角,您将看到它的0,0,如果单击右下角,您将看到4096,4096,这比需要的要晚.这是我的代码

I have tried many things to get the coordinates to map as needed but no luck. Here is my jsfiddle with my example, which has a console.log when you click on the map which shows the coordinates. If you click in the top left you will see its 0,0 and if you click in the bottom right you will see 4096,4096 which is backwards from whats needed. Here is my code that I have

url = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
weight = 4096;
height = 4096;
mapMinZoom = 1;
mapMaxZoom = 7;
map = L.map('map', {
  maxZoom: mapMaxZoom,
  minZoom: mapMinZoom,
  noWrap: true,
  detectRetina: true
});
unproject = function(coord) {
  return map.unproject(coord, 4);
};

southWest = unproject([0, 0]);
northEast = unproject([height, weight]);
map.setMaxBounds(new L.LatLngBounds(southWest, northEast));
tileLayer = L.tileLayer(url, {
  minZoom: mapMinZoom,
  maxZoom: mapMaxZoom,
  noWrap: true,
  tileSize: 256,
  detectRetina: false
}).addTo(map);

map.setView(unproject([0, 0]), 2);

map.on('click', function(e) {
  var coords, latLong;
  latLong = new L.LatLng(e.latlng.lat, e.latlng.lng);
  coords = map.project(latLong, 4);
  console.log("x="+coords.x+", y="+coords.y);
});

推荐答案

如果我理解正确,则需要这样的地图坐标:

If I understand correctly, you would like a map coordinates like this:

[0, 4096] .. [4096, 4096]
.........................
.........................
[0, 0] ........ [4096, 0]

当垂直坐标在上升时增加(例如,在Leaflet上的纬度)而水平坐标在向右移动时(例如在Leaflet的经度上)增加则很好.因此,您似乎不需要反转垂直轴.

It is good as vertical coordinate increases while going up (like latitude on Leaflet) and horizontal coordinate increases while going right (like longitude on Leaflet). So you do not seem to need to invert the vertical axis.

关于坐标的顺序,不幸的是,Leaflet首先使用纬度,然后使用经度,并且几乎不可能在本地进行切换.但是,您可以轻松构建仅交换坐标的包装方法. (类似于具有更简单的CRS和投影功能的传单LatLngBounds ,但无需使用负垂直坐标-y,因为在您的情况下,您希望它与纬度方向相同

As for the order of the coordinates, unfortunately Leaflet uses latitude first, longitude second, and it is almost impossible to switch them natively. However, you can easily build a wrapper method that just swaps the coordinates. (similar to Leaflet LatLngBounds with simpler CRS & projection, but no need to use negative vertical coordinate -y since in your case you want it in the same direction as latitude)

然后另一个问题是您如何希望在这四个角之间的坐标 .默认的CRS(Web Mercator)沿垂直轴不是线性的.如果您需要坐标是线性的,则应使用 L.CRS.Simple .请参阅传单非基于地球的地图的教程.

Then another question would be how you want the coordinates in-between those 4 corners. The default CRS (Web Mercator) is not linear along the vertical axis. If you need your coordinates to be linear, you should use L.CRS.Simple. See Leaflet tutorial for non-Earth-based maps.

这篇关于如何将Leaflet坐标切换到西南0,0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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