2

I think I have two different questions here, I'm building off this question. Id like to take this one step more and convert the px values to true latitude and longitude coordinates.

So I would like to turn 251px N, 10px E to 40° 10' 33'' N, 3° 4' 56'' E

40° = 40 degrees

10' = 10 minutes

33'' = 33 seconds

or 40.133 I think but when minutes go to 60 it kicks the next degree up one. so 41 when its 40.559 + 1 more second. I hope someone understands.

var screenX = $(document).width() / 2;
var screenY = $(document).height() / 2;

$(document).mousemove(function(e){

var apX = screenX - e.pageX;
var apY = screenY - e.pageY;

var latT = (apY>=0) ? 'N' : 'S';
var lonT = (apX>=0) ? 'W' : 'E';

apX = Math.round(Math.abs(apX));
apY = Math.round(Math.abs(apY));

$('#detect').html( apX  + 'px '+ latT +', '+ apY + 'px '+ lonT  );

});

Demo of the above code. Any advice?

Community
  • 1
  • 1
uriah
  • 2,485
  • 5
  • 28
  • 40
  • 1
    There are several unclear things here - to me and possibly to you. 40° 10' 33'' is **not** the same as decimal 40.133, it's 40.175833 (see http://en.wikipedia.org/wiki/Geographic_coordinate_conversion). Also converting "pixels" (Cartesian coordinates) to geological coordinates depends on the projection of the map "underneath". – RoToRa Dec 14 '11 at 11:47
  • I'm trying to convert a webpage to latitude and longitude coordinates. I was hoping to convert the height of a webpage to 100% and work out the two decimal places from there. I don't think I have described it properly, could you edit my question to make it easier to understand? – uriah Dec 14 '11 at 11:55
  • In order to edit it, I'd have to understand your question first. What do you mean with "converting a webpage to latitude and longitude coordinates"? That doesn't make sense. Latitude and longitude are used with maps not web pages... – RoToRa Dec 14 '11 at 11:58
  • Why cant a web page be a map? http://uriahgray.com/cap is a demo – uriah Dec 14 '11 at 12:04
  • I'm not saying a webpage can't be a map, but there is no map in your example. Or is this just "fun" with no actual geographical meaning for the coordinates? – RoToRa Dec 14 '11 at 12:09
  • I guess it just for fun the coordinates reference the current mouse on the webpage. Technically you could use them to reference any point on the site but really its just for appearance yes. The conversion from px to coordinates doesn't need to be exact but I'm not sure of any other way to describe it. – uriah Dec 14 '11 at 12:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5846/discussion-between-uriah-and-rotora) – uriah Dec 14 '11 at 12:15

1 Answers1

1

Something like this should work:

$('#detect').html( toGeo(apX, screenX)  + latT +', '+ toGeo(apY, screenY) + lonT  );

function toGeo(d, max) {
   var c = '';

   var r = d/max * 180;
   var deg = Math.floor(r);
   c += deg + "° ";

   r = (r - deg) * 60;
   var min = Math.floor(r);
   c += min + "′ ";

   r = (r - min) * 60;
   var sec = Math.floor(r);
   c += sec + "″";

   return c;
}

Just as a reminder: These are "fake" coordinates, they won't correspond to a real map.

RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • Fantastic @RoToRa My only concern is that S doesnt trigger until you are at the bottom rather than at the middle. And it has these strange flashes as it changes. http://uriahgray.com/cap/ – uriah Dec 14 '11 at 13:17