0

I am trying to combine the OSM OpenLayers example with the results I got from query.wikidata.org, but it seems that I am doing the wrong transformation. What would be the right transformation of long and lat?

<html><body>
  <div id="mapdiv"></div>
  <script src="http://www.openlayers.org/api/OpenLayers.js"></script>
  <script>
    map = new OpenLayers.Map("mapdiv");
    map.addLayer(new OpenLayers.Layer.OSM());

    var lonLat = new OpenLayers.LonLat( 40.228055555556, 27.242222222222 )
          .transform(
            new OpenLayers.Projection("EPSG:900913"), // transform from WGS 1984
            map.getProjectionObject() // to Spherical Mercator Projection
          );

    var zoom=16;

    var markers = new OpenLayers.Layer.Markers( "Markers" );
    map.addLayer(markers);

    markers.addMarker(new OpenLayers.Marker(lonLat));

    map.setCenter (lonLat, zoom);
  </script>
</body></html>
Spoom
  • 195
  • 3
  • 12
  • 1
    What do you mean by *"it seems that I am doing the wrong transformation"*? What are you expecting to see and what are you actually getting? – kryger Feb 29 '16 at 10:58
  • I am using https://www.openstreetmap.org to see where the long lat actually is. For example battle of Aachen Point(50.766666666667 6.1) was fought in Aachen. So I would expect it to show Aachen, but instead it shows the marker somewhere really different. – Spoom Feb 29 '16 at 11:08

2 Answers2

2

The transformation is wrong: your lonLat variable is in EPSG:4326, so you should transform from EPSG:4326 to EPSG:900913

var lonLat = new OpenLayers.LonLat( 27.242222222222, 40.228055555556 )
      .transform(
        new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
        map.getProjectionObject() // to Spherical Mercator Projection
      );
ahocevar
  • 5,448
  • 15
  • 29
fradal83
  • 2,002
  • 1
  • 10
  • 9
  • Thanks, but I guess it is still wrong, because it should be somewhere in Turkey. http://www.openstreetmap.org/search?query=40.228055555556%2C%2027.242222222222#map=6/40.229/27.235 – Spoom Feb 29 '16 at 16:23
  • 1
    I edited the above answer. The order is (lon, lat), not (lat, lon) like in the previous version of the snippet. – ahocevar Feb 29 '16 at 20:39
0

When using Leaflet it is not necessary to make any transformation.

var map = L.map('map').setView(40.228055555556, 27.242222222222],
  13);

L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

L.marker([40.228055555556, 27.242222222222]).addTo(map)
  .bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
  .openPopup();
Spoom
  • 195
  • 3
  • 12