Convert lat / lon coordinates to pixel?

I am trying to convert a lat / lon pair to a pixel coordinate. I found this Mercator projection, but I do not understand the code. What is the factor variable x_adj, y_adj? When I run the code without these constants, my lat / lon pair is not on my map, and the x and y pixel coordinate is not what I want.

function get_xy(lat, lng) { var mapWidth=2058; var mapHeight=1746; var factor=.404; var x_adj=-391; var y_adj=37; var x = (mapWidth*(180+lng)/360)%mapWidth+(mapWidth/2); var latRad = lat*Math.PI/180; var mercN = Math.log(Math.tan((Math.PI/4)+(latRad/2))); var y = (mapHeight/2)-(mapWidth*mercN/(2*Math.PI)); return { x: x*factor+x_adj,y: y*factor+y_adj} } 

Source: http://webdesignerwall.com/tutorials/interactive-world-javascript-map/comment-page-1?replytocom=103225

[2] Secret latitude / longitude indicates pixels (x, y) on the projection of the Mercator

+6
source share
2 answers

Where did these variables come from

These variables are selected so that they correspond to the calculated coordinates of the background image of the map. If the map projection parameters were known, they could be calculated. But I believe that it is much more likely that they were obtained through trial and error.

How to calculate the Mercator projection

If you want a more general method to describe the world section of a given (non-transverse) Mercator map , you can use this code:

 // This map would show Germany: $south = deg2rad(47.2); $north = deg2rad(55.2); $west = deg2rad(5.8); $east = deg2rad(15.2); // This also controls the aspect ratio of the projection $width = 1000; $height = 1500; // Formula for mercator projection y coordinate: function mercY($lat) { return log(tan($lat/2 + M_PI/4)); } // Some constants to relate chosen area to screen coordinates $ymin = mercY($south); $ymax = mercY($north); $xFactor = $width/($east - $west); $yFactor = $height/($ymax - $ymin); function mapProject($lat, $lon) { // both in radians, use deg2rad if neccessary global $xFactor, $yFactor, $west, $ymax; $x = $lon; $y = mercY($lat); $x = ($x - $west)*$xFactor; $y = ($ymax - $y)*$yFactor; // y points south return array($x, $y); } 

A demo version of this code is available at http://ideone.com/05OhG6 .

Regarding aspect ratio

Customizing with $xFactor != $yFactor creates a kind of stretched Mercator projection. This is no longer conformal (angular conservation). If you need a true Mercator projection, you can omit any of the first six variable assignments, i.e. those that define the bounding box or those that describe the size of the resulting map, and then use some calculations, also select it, satisfying $xFactor == $yFactor . But since the choice to be omitted is arbitrary, I believe that the above code is the most symmetrical way of describing things.

+3
source

Here's how to get the returned variables X and Y from the function you found ...

 var xy=get_xy(56,34); var X=xy.x; var Y=xy.y; 

X and Y now contain the coordinates.

-2
source

Source: https://habr.com/ru/post/953953/


All Articles