OpenLayers LonLat转型

时间:2016-02-29 10:51:31

标签: openlayers openstreetmap wikidata

我正在尝试将OSM OpenLayers示例与我从query.wikidata.org获得的结果结合起来,但似乎我正在进行错误的转换。 什么是long和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>

2 个答案:

答案 0 :(得分:2)

转换错误:你的lonLat变量在EPSG:4326,所以你应该从EPSG:4326转换为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
      );

答案 1 :(得分:0)

使用Leaflet时,无需进行任何转换。

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();