title: Convert latitude/longitude point to a pixels (x,y) on mercator projection
body: <p>I'm trying to convert a lat/long point into a 2d point so that I can display it on an image of the world-which is a mercator projection.</p>
  
  <p>I've seen various ways of doing this and a few questions on stack overflow-I've tried out the different code snippets and although I get the correct longitude to pixel, the latitude is always off-seems to be getting more reasonable though.</p>
  
  <p>I need the formula to take into account the image size, width etc. </p>
  
  <p>I've tried this piece of code:</p>
  
  <pre><code>double minLat = -85.05112878;
  double minLong = -180;
  double maxLat = 85.05112878;
  double maxLong = 180;
  
  // Map image size (in points)
  double mapHeight = 768.0;
  double mapWidth = 991.0;
  
  // Determine the map scale (points per degree)
  double xScale = mapWidth/ (maxLong - minLong);
  double yScale = mapHeight / (maxLat - minLat);
  
  // position of map image for point
  double x = (lon - minLong) * xScale;
  double y = - (lat + minLat) * yScale;
  
  System.out.println("final coords: " + x + " " + y);
  </code></pre>
  
  <p>The latitude seems to be off by about 30px in the example I'm trying. Any help or advice?</p>
  
  <p><em>Update</em></p>
  
  <p>Based on this question:<a href="https://stackoverflow.com/questions/2103924/mercator-longitude-and-latitude-calculations-to-x-and-y-on-a-cropped-map-of-the/10401734#10401734">Lat/lon to xy</a></p>
  
  <p>I've tried to use the code provided but I'm still having some problems with latitude conversion, longitude is fine.</p>
  
  <pre><code>int mapWidth = 991;
  int mapHeight = 768;
  
  double mapLonLeft = -180;
  double mapLonRight = 180;
  double mapLonDelta = mapLonRight - mapLonLeft;
  
  double mapLatBottom = -85.05112878;
  double mapLatBottomDegree = mapLatBottom * Math.PI / 180;
  double worldMapWidth = ((mapWidth / mapLonDelta) * 360) / (2 * Math.PI);
  double mapOffsetY = (worldMapWidth / 2 * Math.log((1 + Math.sin(mapLatBottomDegree)) / (1 - Math.sin(mapLatBottomDegree))));
  
  double x = (lon - mapLonLeft) * (mapWidth / mapLonDelta);
  double y = 0.1;
  if (lat &lt; 0) {
      lat = lat * Math.PI / 180;
      y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);
  } else if (lat &gt; 0) {
      lat = lat * Math.PI / 180;
      lat = lat * -1;
      y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);
      System.out.println("y before minus: " + y);
      y = mapHeight - y;
  } else {
      y = mapHeight / 2;
  }
  System.out.println(x);
  System.out.println(y);
  </code></pre>
  
  <p>When using the original code if the latitude value is positive it returned a negative point, so I modified it slightly and tested with the extreme latitudes-which should be point 0 and point  766, it works fine. However when I try a different latitude value ex: 58.07 (just north of the UK) it displays as north of Spain. </p>