Get X,Y from LAT,LNG custom svg map - javascript

Hi to all i'm developing a map with raphael.js as they do in here : http://raphaeljs.com/world/
the problem is that i'm using a custom svg world map so they function getXY() obviously wont return the correct x,y of a lat,lng.
How can i calculate the x and y of a given lat/lng knowing the map width/height and the rapport froma point and his coordinates (x:1104.73=lng:42.998604)(y:559.25=lat:9.424553).
they use this code:
cx: lon * 2.6938 + 465.4
y: lat * -2.6938 + 227.066
but i cant figure out what is 2.6938 and the other two numbers.
I've also used this math here:
int x = (int) ((MAP_WIDTH/360.0) * (180 + lon));
int y = (int) ((MAP_HEIGHT/180.0) * (90 - lat));
But i cant get it works,anibody can help me?

The formula to use depends on what projection (Equirectangular, Robinson, Miller, Lambert, Mercator, or other one) is used by your map. More on projections here http://en.wikipedia.org/wiki/Map_projection
You will not find any help if you don't know what is the projection for your "custom svg world map"
Cheers

Related

Converting Latitude and Longitude to XY Coordinates

I am doing a Javascript application that requires the movement of a certain element in a real world map, in frames.
For each frame, i have the following positions in latitude and longitude for the element, for example for frame 0:
- Latitude: 49.011213
- Longitude: 8.422885
For frame 1:
- Latitude: 49.01121
- Longitude: 8.422887
Frame (Frame 0) needs to be point (0,0) and I want the following ones to be converted to XY Coordinates as well.
Basically, I need a Javascript script that receives the latitude and longitude of a frame and returns the position (x,y) for that frame (in relation to frame 0 with position (0,0)).
I've tried the following but it doesn't work:
function convertSphericalToCartesian(latitude, longitude)
{
// Convert from Degrees to Radians
let latRad = latitude * (Math.PI)/180;
let lonRad = longitude * (Math.PI)/180;
let earthRadius = 6367; // Radius in km
let posX = earthRadius * Math.cos(latRad) * Math.cos(lonRad);
let posY = earthRadius * Math.cos(latRad) * Math.sin(lonRad);
return {x: posX, y: posY};
}
Do you know a better formula for this? Thank you very much.
Any conversion you do will introduce error, since you're trying to map the surface of a sphere to rectangular coordinates. You haven't mentioned what you're doing with this information - the mention of "frames" makes me think of animation or a game involving moving things on a map. If that's the case, then your calculations are going to be closely tied to the map you're using.
In general, the question is: how far apart are your coordinates likely to be?
For small distances, up to a few miles or kilometers, you're probably just fine treating the lat/lon as x/y coordinates. For example, the coordinates you've given in your example are literally only feet apart - don't bother with complicated calculations. And again, how you draw something on a given map at those coordinates depends very much on the map you're using.
For larger distances, the curvature of the Earth becomes important and the map projection you're working with will make a big difference in how those coordinates are calculated. For example, this question discusses the calculations for a Mercator projection.

atan() of a number in JavaScript when transforming x,y coordinates to longitude and latitude

I am using the Web Mercator to transform longitudes and latitudes to x and y coordinates. However, when I want to inverse this problem the built-in atan() (or Math.atan()) function of JavaScript (and p5.js) gives me trouble. The Web Mercator and the calculated inverse are given below.
formulas
Then, for an example y position in my grid, I have code similar to:
//Sample code for transforming y coordinate to latitude using Web Mercator
//map size: 128x128 -> c_map = 128, zoom level: 1
var ypos = 100;
var c_map = 128;
var temp = PI*(1- ypos/(pow(2, 1)*c_map);
temp = atan(exp(temp) * 180/PI);
lat = 2*(180 / PI)*(temp - PI / 4);
However, this gives me false values for the y coordinate since JavaScript computes the tangent and arctangent in radians.
I have tried several configurations of first transforming the exponent to radians (is the exponent in degrees?) and then transforming the result back to degrees, but to no avail. The y coordinates are given relative to the point [0,0].
What kind of radian/degree transformation do I need to apply where in order to get correct values of my y coordinate?

Converting Latitude, Longitude in Decimals to the pixels for plotting markers on a plane svg image

I can see many answers to this question, but I don't clearly seem to follow most of those.
What I have been trying is to create an interactive world map with dynamic markers based on given latitudes and longitudes.
I have tried various equations found online but couldn't get the closer result.
The things I have tried so far,
var y = Math.round(((-1 * lat) + 90) * (containerHeight / 180));
var x = Math.round((lon + 180) * (containerWidth / 360));
x = R * Math.cos(lat) * Math.cos(lon)
y = R * Math.cos(lat) * Math.sin(lon)
var x = 1.9928145063077821*1000000 - 14017.262662576404*lat + 19202.720184031*lon;
var y = 334427.99241686985 - 33618.80202858345*lat - 14069.747257820272*lon;
Is there any better of implementing the same? Mercator, Azimuthal, Cylindrical ?
You can search for "Web Mercator". Google, Bing, OpenStreetMap uses this one and its kind a standard for web now. I don't have experience and enough information about calculation methods but formula which given on Wikipedia may help you.

Get center LatLng point of a custom tile in Google Maps API v3

I've got a custom map type API I need to talk to.
I'd like to get the map images by using the 'tile' function of Google Maps. I've got the tiles setup like this: https://developers.google.com/maps/documentation/javascript/examples/maptype-overlay?hl=nl
The API to get the different tileimages requires me to have a center point for each of the tiles.
So how do I get/calculate the center LatLng point for each tile?
Ok I figured it out.
Here's what I did.
You have a tile coordinate for each tile (x,y) and zoom and a tile size.
So first we make a new Google MapsPoint to the center point of the tile:
var centerpoint = new google.maps.Point((coord.x * tileSize) + (tileSize / 2), ((coord.y + 1) * tileSize) + (tileSize / 2));
Then convert those x,y coordinates to LatLng (conversion functions and the math behind it can be found on http://www.maptiler.org)
So it was actually pretty simple...
You can calculate it also directly:
n = 2 ^ zoom
lon_deg = xtile / n * 360.0 - 180.0
lat_rad = arctan(sinh(π * (1 - 2 * ytile / n)))
lat_deg = lat_rad * 180.0 / π
Source and code samples in various languages: http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat.

Getting Y position from Latitude on a spherical projected map

In Javascript.
I have a map of the world that is 1024 x 1024px - It is a spherical Mercator projection.
Im looking to convert long/lat to x,y for this map. I have a bunch of coordinates that I need to overlay on top of it.
Calculating longitude was very easy, and I am doing it like so:
pos.x = ((long + 180)/ 360) * 1024;
I just need to same for lat --> y
I looked around a saw a lot of reference to openlayers, but didn't see such a conversion.
To boil it down:
var mercator = function(lat, lng) {
return [lng, Math.log( (Math.sin(lat) + 1.0) / Math.cos(lat)) ] ;
};
Lat and long must be expressed in radians of course.
You can find the corresponding formulas in this wikipedia article: Mercator Projection.

Categories

Resources