[Talk-co] coordenadas en wgs84 en openlayers-osm

Germán Márquez Mejía manchito en gmail.com
Mar Jun 8 21:02:02 BST 2010


El problema se debe a que el control no es el que determina el sistema 
de coordenadas sino la capa activa. La mayoría de capas vienen por 
defecto en WGS84, pero las de OSM viene con proyección S-Mercator. El 
handler sólo imprime lo que recibe de la capa. De ahí la diferencia 
entre lo que saca [0] y lo que saca el código de abajo, ya que en [0] la 
capa base es un WMS y no Mapnik.

Por lo tanto hay que transformar todo lo que entre y salga del mapa, 
como en map.setCenter(), sólo que, para imprimirlas, tocaría lo 
contrario, algo así como:

var lonlat = map.getLonLatFromViewPortPx(e.xy).transform(
	new OpenLayers.Projection("EPSG:900913"),
	new OpenLayers.Projection("EPSG:4326"));

Si después se quiere aregar una capa que por defecto viene en WGS84, se 
le pasa la opción sphericalMercator: true. Por ejemplo:

var layerYahoo = new OpenLayers.Layer.Yahoo("Yahoo Maps", {
		sphericalMercator: true
	});

Y listo. De lo contrario no coincidirían.

En resumen, el código quedaría así:

var map;

OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
   defaultHandlerOptions: {
     'single': true,
     'double': false,
     'pixelTolerance': 0,
     'stopSingle': false,
     'stopDouble': false
   },

   initialize: function(options) {
     this.handlerOptions = OpenLayers.Util.extend(
       {}, this.defaultHandlerOptions
     );
     OpenLayers.Control.prototype.initialize.apply(
       this, arguments
     );
     this.handler = new OpenLayers.Handler.Click(
       this, {
	'click': this.trigger
       }, this.handlerOptions
     );
   },

   trigger: function(e) {
     var lonlat = map.getLonLatFromViewPortPx(e.xy).transform(
       new OpenLayers.Projection("EPSG:900913"),
       new OpenLayers.Projection("EPSG:4326")
     );
     alert("You clicked near " + lonlat.lat + " N, " +
       + lonlat.lon + " E");
   }
});

function init() {
   map = new OpenLayers.Map("basicMap");
   var mapnik = new OpenLayers.Layer.OSM();
   map.addLayer(mapnik);
   map.setCenter(new OpenLayers.LonLat(13.41,52.52) // Center of the map
     .transform(
       new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
       new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator 
Projection
     ), 15 // Zoom level
   );
   var click = new OpenLayers.Control.Click();
   map.addControl(click);
   click.activate();
}

Espero que sirva.

Saludos,

Mancho

Am 08.06.2010 19:28, schrieb ouɐɯnH:
> Hola
>
> Mirando el ejemplo de openlayers para capturar coordenadas con un
> click [0] y combinando lo con el ejemplo simple de la wiki de OSM [1]
> no puedo hacer que me muestre las coordenadas en wgs84 para grabarlas
> posteriormente.
>
> Seguro que alguien de la lista ya lo ha hecho  y pido colaboración.
>
> salu2
> Humano
> PD: pego el codigo abajo
>
>
> [0] http://openlayers.org/dev/examples/click.html
> [1] http://wiki.openstreetmap.org/wiki/OpenLayers_Simple_Example
>
>
> <html>
>    <head>
>      <title>OpenLayers Demo</title>
>      <style type="text/css">
>        html, body, #basicMap {
>            width: 100%;
>            height: 100%;
>            margin: 0;
>        }
>      </style>
>      <script src="OpenLayers/lib/OpenLayers.js"></script>
>      <script>
>      OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
>                  defaultHandlerOptions: {
>                      'single': true,
>                      'double': false,
>                      'pixelTolerance': 0,
>                      'stopSingle': false,
>                      'stopDouble': false,
>                      'projection': "EPSG:4326"
>                  },
>
>                  initialize: function(options) {
>                      this.handlerOptions = OpenLayers.Util.extend(
>                          {}, this.defaultHandlerOptions
>                      );
>                      OpenLayers.Control.prototype.initialize.apply(
>                          this, arguments
>                      );
>                      this.handler = new OpenLayers.Handler.Click(
>                          this, {
>                              'click': this.trigger
>                          }, this.handlerOptions
>                      );
>                  },
>
>                  trigger: function(e) {
>                      var lonlat = map.getLonLatFromViewPortPx(e.xy);
>                      alert("You clicked near " + lonlat.lat + " N, " +
>                                                + lonlat.lon + " E");
>                  }
>
>              });
>        function init() {
>          map = new OpenLayers.Map("basicMap");
>          var mapnik = new OpenLayers.Layer.OSM();
>          map.addLayer(mapnik);
>          map.setCenter(new OpenLayers.LonLat(13.41,52.52) // Center of the map
>            .transform(
>              new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
>              new OpenLayers.Projection("EPSG:900913") // to Spherical
> Mercator Projection
>            ), 15 // Zoom level
>          );
>          var click = new OpenLayers.Control.Click();
>                  map.addControl(click);
>                  click.activate();
>        }
>      </script>
>    </head>
>    <body onload="init();">
>      <div id="basicMap"></div>
>    </body>
> </html>
>





More information about the Talk-co mailing list