1

hi I have image of my house.Top view image.I want to have latitude lotitude info displayed when i click on the image. I do have latitude longitude value for 1 left top part of image.

Also how to maintain latitude longitude values while zooming in out of the image.

richi arora
  • 273
  • 4
  • 11

2 Answers2

1

Lat/lon is a geodesic coordinate system (WGS84), which means it is curved coordinates going around the earth - an image is flat, which means typically you can't easily go directly between the two. However it may be the case that an image of your house is so small area, that the calculation error will be small enough to be negligible (depending on what you need it for).

To do what you want to do, you need to find a "degrees per pixel" value which means you need to know the lat/lon for both top/left and bottom right of your image. If you have that it's simple. This assumes you're in the northern hemisphere:

var degreesPerPixelX = bottomX - topX / imageWidth;
var degreesPerPixelY = bottomY - topY / imageHeight;

And an event handler (the getEventOffsetFromImageXXX are not shown).

function onClick (evt) {
    var x = getEventOffsetFromImageLeft(evt);
    var y = getEventOffsetFromImageTop(evt);

    var clickedLon = topX + x * degreesPerPixelX;
    var clickedLat = bottomY + y * degreesPerPixelY;
}

The zoom level will affect the top/left bottom/right lon/lat so the calculations need to adjust accordingly.

When Google Maps calculate x/y to lon/lat they internally ALWAYS first convert the lon/lat to the coordinate system Spherical Mercator (EPSG:900913), do the operations in that system and then convert back. However Spherical Mercator has fixed zoom levels, which is probably not right for you. Nevertheless, this is a very worthwhile read.

http://www.maptiler.org/google-maps-coordinates-tile-bounds-projection/

N.b. degreesPerPixel is called resolution in google talk and the unit is meters per pixel. Meter is the unit in Spherical Mercator, which roughly translates to a meter at the equator, but is far from a meter the further north/south you get.

Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • hi thanx for the quick reply..while searching wat i want i landed up here http://stackoverflow.com/questions/1019997/in-javascript-convert-lat-longs-to-x-y-co-ordinates i checked the example of mercator projection and talied the output with google earth it does seem ok to me..However want to know the meaning of longitude_shift used here – richi arora Nov 21 '10 at 09:10
  • Also if I want to show longitude latitude while hovering over each pixel position how to move in this direction..I mean while hovering i get pixel position now i want to convert it into the latitude longitude dynamically...presuming i have topleft longitude latitude only so how to get it..though m not in favour of provideing zoom functionality at such a basic stage.Wats ur take on the formula used in above link that i have mentioned in comment – richi arora Nov 21 '10 at 09:14
  • Hi, I don't think I see the link with the longitude_shift - is it missing? – Martin Algesten Nov 21 '10 at 14:23
  • You would always need to know what a pixel means in latitude/longitude. That's what the **degreesPerPixel** is (degrees being the unit of lat/lon). So just knowing one lat/lon will never be enough, you need to know something like top/left AND bottom/right. – Martin Algesten Nov 21 '10 at 14:25
  • can u plz provide example for code given? how about this code – richi arora Nov 22 '10 at 02:26
0

Anyone how abt this code snippet

function longToX(longitudeDegrees)
{
var longitude=longitudeDegrees-baselong;
longitude =degreesToRadians(longitude);
return (radius * longitude);
}
function latToY(latitudeDegrees)
{
var latitude=latitudeDegrees-baselat;
latitude =degreesToRadians(latitude);
var newy = radius/2.0 * 
Math.log( (1.0 + Math.sin(latitude)) /
(1.0 - Math.sin(latitude)) );
return newy;
}
function xToLong(xx)
{
var longRadians = xx/radius;
var longDegrees = radiansToDegrees(longRadians);
var rotations = Math.floor((longDegrees + 180)/360)
var longitude = longDegrees - (rotations * 360)
return longitude+baselong;
 }
 function yToLat(yo)
 {
var latitude =  (Math.PI/2) - (2 * Math.atan(Math.exp(-1.0 * yo /this.radius)));
return radiansToDegrees(latitude)+baselat;
}  
Muad'Dib
  • 28,542
  • 5
  • 55
  • 68
richi arora
  • 273
  • 4
  • 11