My idea is plain and simple:
- I've got a map as an image, that can be either a scan of a printed map, or a SVG map from Wikipedia, or a screenshot of Google Maps.
- I have for that map two lat/lon coordinates, and the user clicks on their position on the map, so I know their corresponding X/Y position on the image.
Now the fun part: I need to place a new marker on this map, only knowing its lat/lon coordinates.
I can't use any Google Maps API or other JS api as the maps can be of any scale, and often won't have a corresponding zoom factor.
I believe I need to find the image boundaries to position a marker on the map, and for this I need to know the scale of my map. I think I'm getting somewhere with this code, but I'm not getting accurate results for NE longitude.
// Get NE and SW boundaries for the two known markers
var ne = {lat: Math.max(map.a.lat, map.b.lat), lon: Math.max(map.a.lon, map.b.lon),
x: Math.min(pos.a.x, pos.b.x), y: Math.min(pos.a.y, pos.b.y)};
var sw = {lat: Math.min(map.a.lat, map.b.lat), lon: Math.min(map.a.lon, map.b.lon),
x: Math.max(pos.a.x, pos.b.x), y: Math.max(pos.a.y, pos.b.y)};
// Get boundaries box width and height
var box = {width: sw.x - ne.x, height: sw.y - ne.y};
box.lat_delta = ne.lat - sw.lat;
box.lon_delta = ne.lon - sw.lon;
// Map scale
box.lat_ratio = box.lat_delta / box.height;
box.lon_ratio = box.lon_delta / box.width;
// Get Top-Left and Bottom-Right coordinates of the map
var tl = {
lat: ne.lat + (box.lat_ratio * ne.y),
lon: ne.lon - (box.lon_ratio * ne.x)};
var br = {
lat: sw.lat + (box.lat_ratio * (box.height - sw.y)),
lon: sw.lon - (box.lon_ratio * (box.width - sw.x))};
And then, how do I get the X/Y coordinates of the third marker? I thought about doing something like that:
var x = (map.c.lon - tl.lon) * box.lon_ratio; var y = (map.c.lat - tl.lat) * box.lat_ratio;
But results don't make any sense. So I'm a bit lost there.