admin管理员组文章数量:1401849
The goal is to use an array of coordinates to draw a route without using Google Maps. What is the best way to convert standard Latitude/Longitude coordinates to HTML5 canvas coordinates?
The goal is to use an array of coordinates to draw a route without using Google Maps. What is the best way to convert standard Latitude/Longitude coordinates to HTML5 canvas coordinates?
Share Improve this question asked Jul 22, 2016 at 13:18 Noel BaronNoel Baron 7366 silver badges17 bronze badges 3- Have you looked at the Haversine formula? It will give you distance and angle, which you can convert to x, y easily. – Yimin Rong Commented Jul 22, 2016 at 13:29
- I posted an answer that does it pretty well. – Noel Baron Commented Jul 22, 2016 at 19:50
- 1 Note the weirdness that happens at the date line (i.e. 180th meridian). Fortunately, there aren't too many roads that cross it, currently only Fiji. But, depending on how large an area you want to deal with, you might need to code the special case where the shorter distance uses reversed coordinates. – Yimin Rong Commented Jul 22, 2016 at 20:42
1 Answer
Reset to default 9I found a fairly simple way to convert lat/lon to points.
- Get the min/max coordinates of your set.
- Have your canvas dimensions handy.
- Use floating point calculation to get percentage of x and y on canvas.
- Provide points to D3.
See JS Fiddle: https://jsfiddle/12stct3y/
var bounds = {
"minLon": -81.267555236816,
"maxLon": -81.261039733887,
"maxLat": 28.979709625244,
"minLat": 28.977783203125
}
var dimensions = {
width:400,
height:150
}
function getX(x) {
var position = (x - bounds.minLon) / (bounds.maxLon - bounds.minLon);
return dimensions.width*position;
}
function getY(y) {
var position = (y - bounds.minLat) / (bounds.maxLat - bounds.minLat);
return dimensions.height*position;
}
Not included in fiddle:
- Rotating to north-based view
- Creating dimensions based on variable bounds
版权声明:本文标题:javascript - How to convert latitudelongitude to HTML5 coordinates without using Google Maps? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744306982a2599843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论