0

I try to convert lat-lon values to pixels, I tried this excellent example, here the solution (converted to JS):

<div id="dz" style="background-image: url('algerie.png');height: 558px;width: 571px;background-size: contain;background-repeat: no-repeat;">
<p id="hey">0</p>       
</div>
<script>
function placeDiv(x_pos, y_pos) {
var d = document.getElementById('hey');
d.style.position = "absolute";
d.style.left = x_pos+'px';
d.style.top = y_pos+'px';
}   
var mapWidth = 571;
var mapHeight = 558;
var mapLonLeft = -8.66884;
var mapLonRight = 11.99733;
var mapLonDelta = mapLonRight - mapLonLeft;
var mapLatBottom = 18.96815;
var mapLatBottomDegree =mapLatBottom * Math.PI / 180;

function convertGeoToPixel(lat, lon)
{

x = (lon - mapLonLeft) * (mapWidth / mapLonDelta);

lat = lat * Math.PI / 180;
worldMapWidth = ((mapWidth / mapLonDelta) * 360) / (2 * Math.PI);
mapOffsetY = (worldMapWidth / 2 * Math.log((1 + Math.sin(mapLatBottomDegree)) / (1 - Math.sin(mapLatBottomDegree))));
y = mapHeight - ((worldMapWidth / 2 * Math.log((1 + Math.sin(lat)) / (1 - Math.sin(lat)))) - mapOffsetY);

return placeDiv(x, y);
}
</script>

But I dont get accurate values; if I test this:

enter image description here

I get:

enter image description here

As you can see, the marker (I use a simple number) was out.

Update: By the way, I used MOBAC to get the further extreme point (the image is just illustrative, because the point, I got it with the highest zoom to get good precision)

enter image description here

Community
  • 1
  • 1
Abdelouahab
  • 7,331
  • 11
  • 52
  • 82
  • 1
    Pixels in what projection? You appear to be just treating the map boundaries as X/Y values (an [Equirectangular projection](https://en.wikipedia.org/wiki/Equirectangular_projection). If you want accuracy you need to take the projection into account. Something like [Proj4js](http://proj4js.org/) would answer the question. – jwd630 Apr 06 '15 at 17:06
  • can you make an example so I can validate the answer? – Abdelouahab Apr 06 '15 at 20:45

0 Answers0