Does anybody know how to convert latitude,longitude coordinates into a pixel(x,y) coordinate?
I have a worldmap (1918x960 pixels) and I have a database of cities containing lat,lon coordinates. I searched the internet and found a lot of info about this topic. Only, most solutions are using libraries in another language (pyton,Java,Javascript)
Is there a method available in xojo to convert these coords into pixels? I searched in this forum, but was not able to find a solution without using a google maps api.
Anyone does have experience with this and want to share?
it’s a simple linear method.
depends on what your map is really but something like :
180°W to 180°E goes linear from 0 to 1918 pixels
and -90° to 90° goes linear from 0 to 960 pixels
nothing complicated maths in that.
changing coordinates from gps to lambert for example is far more complicated …
Oh, really so easy. I thought about this too, but the articles on the internet were so extended and the formula’s used so big, that I understood it must be more complex than I initially thought.
Thanks for the reply
I found a picture on the internet of a worldmap 1918x960 pixels. All I want is a formula. I am busy to sort out a calculation, but seeing the amount of suggested solutions on the internet, I am afraid it is not that obviously easy as I thought it is
Unfortunately it’s not linear… The lines of longitude are closer together near the poles than they are at the equator. Heck if you’re standing at the north or South Pole, you can literally step between them whereas at the equator they are 111.320 KM apart. If you want the math involved, check out this Wikipedia article.
as mentioned Latitude is “linear” as it describes circles of varying diameter from the north to the south pole… there for you can convert Min/Secs of Latitude to degrees and your pixel is then (graphic_width/360)*degree
But for Longitude you need to apply some Geometry… AND assume the earth is a perfect sphere (which is probably good enough for the level of accuracy you should expect)… But you need to apply sin/cos functions to the degree of longitude as a function of the point on the earth in relation to the center of the earth (ie. radius). It is quite doable, just requires that geometry background
I use the formula in my google map viewer control over at XDS…
it’s called the “Haversine” formula
a = sin²(??/2) + cos ?1 ? cos ?2 ? sin²(??/2)
c = 2 ? atan2( ?a, ?(1?a) )
d = R ? c
where ? is latitude, ? is longitude, R is earths radius (mean radius = 6,371km);
note that angles need to be in radians to pass to trig functions!
It’s been used by sailors to map bearings and distance for ages . You can find latitude, longitude, and distance using the formula.
The map type you want for the simple linear mapping is Equirectangular, the first one in Normans link. If the longitude and latitude lines are all parallel and each equally spaced then that’s it. The north and south poles will be really spread out, but check that the latitude goes all the way to ±90 degrees. When I did this the map I used was Equirectangular but didn’t go all the way to the poles.
If the lat/lon is expressed like 43°38’19.39" (Degrees°Minutes’Seconds") you will first need to split it up and convert to flat degrees
Decimal value = Degrees + (Minutes/60) + (Seconds/3600)
Using that first map from Normans link I used this code to plot Santa Cruz, California. This is assuming 0 latitude, 0 longitude is the center of the map and that it goes all the way to ±90 latitude, which that map is.
[code]Sub Paint(g As Graphics, areas() As REALbasic.Rect)
g.DrawPicture map, 0, 0
dim scLat As Double = 36.97417 //Santa Cruz’s lat/lon already converted to flat degrees
dim scLon AS Double = -122.02972
dim centerX As Double = map.Width / 2 //maps center, also half sizes
dim centerY As Double = map.Height / 2
dim latScaling As Double = centerY / 90 //scales degree range to half sizes
dim lonScaling As Double = centerX / 180
dim x As Double = centerX + scLon * lonScaling //compute
dim y As Double = centerY - scLat * latScaling
g.ForeColor = &c00FF00 //draw green crosshair on point
g.DrawLine(x-5, y, x+5, y)
g.DrawLine(x, y-5, x, y+5)
End Sub[/code]
Notice y is calculated as subtracting scLat * latScaling because positive Y goes down in Xojo Graphics coordinates and the subtraction inverts that so it goes the right way, upwards.
Hi Will, thanks for the explanation. I will use your comnent and solution to see if this fits as the solution I am looking for.
It will take some time when I come back with feedback, because this holiday period is a hectic period in which I will have not much time to try things out. After the x-mas days I will have more time and I will be back with my findings.
For everybody else responding on my thread, thank you very much and have all happy holidays
Hi Will, I tried your method and when I try to run it it expects a paint object, not a canvas. Any thoughts?
There is no way one can calculate coordinates into x,y pixel position? I want to print a small icon on the given position.
Someting like Lat 18.56, Lon -66.34 -> calculated to x,y (top/left?) pixel position on the map
All I want is an easy way to point an icon on a position, It does not have to be this narrow, but it must be in the area. When people click on the icon more info will be shown and google earth will show the exact location + info based on the coords. (Lat/Lon)
The last part with google earth is already running very well, but I want to show an overview map with icons pointing to locations with issues.
as said above, it depends on the projection your map is using
it will be ok for longitude, but for latitude we need the type of projection the map is using.