#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0

    Convert (lat, long) gps coordinate to pixels


    I need a function in javascript to convert a latitude, longitude value to a pixel representation within a certain box.

    I have multiple geo locations that I need to represent on the screen in a bounding box of 700 x 664 pixels.

    But I cannot figure out how to convert and then scale the gps coordinates to fit within this box.

    Any help would be appreciated. I found this code snippet somewhere but this does not help to put the pixels within the box:

    Code:
    MAP_HEIGHT = 664;
    MAP_WIDTH = 700;
    
    function convert(lat, lng) {
    var x = ((-1 * lon2) + 90) * (MAP_HEIGHT / 180);
    var y = (lat2 + 180) * (MAP_WIDTH / 360);
    return {
    	 x : x,
    	 y : y
            };
    }
    P.S The gps coordinates that I have do not vary by much, they are like this:

    41.34567, 41.34678, 41.34123456 etc. so to the 0.01 decimal point. I hope you get what I am trying to explain.

    Thanks.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    What scale is the map? What projection does it use? What precision do you require? Is the map just a static image or are you getting it from something Javascript-based?
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by requinix
    What scale is the map? What projection does it use? What precision do you require? Is the map just a static image or are you getting it from something Javascript-based?
    I am reading the latitude longitude from a json file and have to convert it to appropriately scaled images on a view of 700 x 664 pixels.
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    But the projection... The projection is how the map is converted from a 3D source to a 2D map. Examples:



    Unless you have some sort of projection where the lines of latitude and longitude are evenly spaced ("cylindrical" I believe) then you'll have to do some sort of potentially complicated math to convert lat/long into X/Y. Certainly not something as simple as what you have now.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Posts
    4
    Rep Power
    0
    Oh, I do not have the projections with me. Can we assume some projection values i.e the mercator projection?
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,250
    Rep Power
    9400
    Originally Posted by vjoshi
    Can we assume some projection values i.e the mercator projection?
    No. You need to know that information.

IMN logo majestic logo threadwatch logo seochat tools logo