我如何将一个点从[x,y]坐标投影到传单中的LatLng
How do I project a point from [x,y] coordinates to LatLng in Leaflet?
我使用的是传单1.0.0rc3,需要使用绝对像素值来修改我的地图上的东西。因此,我想知道用户在像素中单击的位置,然后将其转换回LatLng
坐标。我尝试使用map.unproject()
,这似乎是正确的方法(unproject()传单文档)。但该方法得到的LatLng值与e.latlng
的输出值有很大的不同。(如输入LatLng (52, -1.7)
,输出LatLng (84.9, -177)
)。所以我一定是做错了什么。
问题:点从层(x,y)空间投影到LatLng空间的正确方法是什么?
这里有一个代码片段(fiddle: https://jsfiddle.net/ehLr8ehk/)
// capture clicks with the map
map.on('click', function(e) {
doStuff(e);
});
function doStuff(e) {
console.log(e.latlng);
// coordinates in tile space
var x = e.layerPoint.x;
var y = e.layerPoint.y;
console.log([x, y]);
// calculate point in xy space
var pointXY = L.point(x, y);
console.log("Point in x,y space: " + pointXY);
// convert to lat/lng space
var pointlatlng = map.unproject(pointXY);
// why doesn't this match e.latlng?
console.log("Point in lat,lng space: " + pointlatlng);
}
你只是使用了错误的方法。要在传单中将图层点转换为LatLng
,您需要使用map.layerPointToLatLng(point)
方法。
// map can capture clicks...
map.on('click', function(e) {
doStuff(e);
});
function doStuff(e) {
console.log(e.latlng);
// coordinates in tile space
var x = e.layerPoint.x;
var y = e.layerPoint.y;
console.log([x, y]);
// calculate point in xy space
var pointXY = L.point(x, y);
console.log("Point in x,y space: " + pointXY);
// convert to lat/lng space
var pointlatlng = map.layerPointToLatLng(pointXY);
// why doesn't this match e.latlng?
console.log("Point in lat,lng space: " + pointlatlng);
}
和一个改变了的jsFiddle。
您也可以查看传单提供的转换方法以获得额外参考。
相关文章:
- EaseJS拖放;放下(动画CC)电影剪辑的鼠标坐标
- 如何在d3.js中返回路径的y坐标
- 在谷歌地图上获取事件的x,y坐标
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 当带有渲染器的DOM元素不在屏幕顶部时,移动了场景的坐标
- 如何在JS Leatflet绘图插件中获取圆的坐标
- 点击(右键点击)使用传单地图库获取图像覆盖的像素坐标
- 查找带有边框的HTML5 Canvas(点击)事件的坐标
- HTML5获取弧的坐标's结束
- 如何在OpenLayers中获取动态绘制的多边形的坐标
- 获取用户位置并将坐标保存在数据库中
- fabric js多边形集合坐标
- OpenLayers:自动将 EPSG 4326 - 文本图层的坐标转换为地图的投影 (EPSG:900913)
- 如何在传单中投影像素坐标
- 将 GeoJSON 坐标转换为其他投影以用于草坪.js
- 矩形坐标投影到另一个矩形中
- 在阿尔伯斯投影地图上将纬度/经度转换为X/Y坐标
- 为什么openlayers在地图点击时返回投影坐标
- 转换ArcGis地理坐标长wkid: 4326投影坐标wkid: 102000/3857
- 我如何将一个点从[x,y]坐标投影到传单中的LatLng