I have written some code which offset a certain latitude and longitude with a distance in centimeters. What I am trying to do is based on a coordinate and a vector create two new points along the line described by the vector.

I believe I have succeeded in doing this but the results I am getting are a bit odd. I am not very experienced with map coordinates and projections so I am posting here to see if my results 'look' right.

The following link shows an image I have of two lines: https://dl.dropboxusercontent.com/u/4467680/LineCheck.png

One extends mostly along the longitude and the other mostly along the latitude. Both lines should have a length of 1 meter. However the red line is noticeably longer than the green line.

The lines are located around longitude 6.080695759673799 and latitude 52.501029191616425 (The Netherlands). I know with the projection I am using (Web Mercator I believe) landmasses become stretched along the poles. Is this what I am seeing with the red line? Is the red line stretched because we are closer to the poles than we are at the equator?

I use a combination of two functions to offset the latitude and longitude. I found the code in this forum post: https://sharpmap.codeplex.com/discussions/395118 I've then rewritten the code to Javascript like this:

function toMercator(latLong)
    var x = latLong.y * 20037508.34 / 180;
    var y = Math.log(Math.tan((90 + latLong.x) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;

    return {x: x, y: y};

function toLatLong(mercator) 
    var lon = (mercator.x / 20037508.34) * 180;
    var lat = (mercator.y / 20037508.34) * 180;

    lat = 180/Math.PI * (2 * Math.atan(Math.exp(lat * Math.PI / 180)) - Math.PI / 2);

    return {
        x: lat,
        y: lon

To offset the point I do this:

var lat = 2;
var long = 5;

var xOffsetCM = 50;
var yOffsetCM = 50;

var mercator = toMercator({x: lat, y: long});
// Add offset
mercator.x += normalizedVector.x * (50 / 100); // divide offset by 100 to get meters
mercator.y += normalizedVector.y * (50 / 100);

var latLong = toLatLong(mercator);

After the last function call latLong should contain the new coordinate.

  • It may help to post a screen shot of the image. I cannot access dropbox from here. :/
    – MaryBeth
    Jul 16, 2014 at 14:28
  • can you add how you calculate the distance in centimeters?
    – AndreJ
    Jul 16, 2014 at 14:37
  • I included a code sample in my original post
    – Mathyn
    Jul 16, 2014 at 14:49
  • The last time I visited the Netherlands the climate suggested it is much further north than 6 degrees latitude!
    – whuber
    Jul 16, 2014 at 15:48

1 Answer 1


Web Mercator has only real metres as units close to the equator. The more to the poles you come, the more x and y distances get apart.

You should better use a "good" projection like the UTM zone of your area if you want to see the lines in correct length.

  • I am currently using Open Layers 3 to render the map so I am not sure if it is possible to use multiple projections for different map areas. We are aiming for worldwide coverage so I'm guessing if I use a different projection for The Netherlands the projection will be wrong for, say, Brazil. Right?
    – Mathyn
    Jul 16, 2014 at 14:52
  • Correct, you can not have a projection with true distances covering the whole world. You can have an overview of available projections here: radicalcartography.net/?projectionref. All have their pros and cons...
    – AndreJ
    Jul 16, 2014 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.