Hello all.<br><br>Im trying to get the coordinates drawn by the drawfeature avaliable in the EditingToolbar in a google maps base layer-<br>I´ve created the map with the "sphericalMercator: true," and im transforming the projection to get the coordinates.<br>
However the numbers returned are far from the truth and even NaN.<br><br>From what ive read its all about the projections and the transforms, but i cant get it to work.<br><br>Below is a working (returning the wrong coordinates) sample code.<br>
<br>Ill apreciate the help.<br><br>Thanks<br>Filipe<br><br><br><html><br> <head><br> <!-- OpenLayers core js --><br> <script type="text/javascript" src="<a href="http://www.openlayers.org/dev/OpenLayers.js">http://www.openlayers.org/dev/OpenLayers.js</a>"></script><br>
<!-- OpenStreetMap base layer js --><br> <script type="text/javascript" src="<a href="http://www.openstreetmap.org/openlayers/OpenStreetMap.js">http://www.openstreetmap.org/openlayers/OpenStreetMap.js</a>"></script><br>
<!-- Google Maps --><br> <script src="<a href="http://maps.google.com/maps/api/js?sensor=false">http://maps.google.com/maps/api/js?sensor=false</a>"></script><br> <br> <script type="text/javascript"><br>
function init() {<br> //<br> var proj = new OpenLayers.Projection("EPSG:4326");<br> <br> //Create map<br> var map = new OpenLayers.Map('map');<br>
//Add controls<br> map.addControl(new OpenLayers.Control.LayerSwitcher());<br><br> <br> //base layer WITH sphericalMercator<br> var gmap = new OpenLayers.Layer.Google("Google Streets", {<br>
sphericalMercator: true,<br> 'maxExtent': new OpenLayers.Bounds(-20037508.34, -20037508.34, 20037508.34, 20037508.34)<br> });<br> //Vector Layer<br> var pointLayer = new OpenLayers.Layer.Vector("Point Layer");<br>
<br> //add layers<br> map.addLayers([gmap, pointLayer]);<br> <br> //center map<br> var lonlat = new OpenLayers.LonLat(16.373056, 48.208333);<br> lonlat.transform(proj, map.getProjectionObject());<br>
map.setCenter(lonlat, 5);<br><br> //add feature<br> var point = new OpenLayers.Geometry.Point(16.373056, 48.208333);<br> point = point.transform(proj, map.getProjectionObject());<br>
var pointFeature = new OpenLayers.Feature.Vector(point, null, null);<br> pointLayer.addFeatures([pointFeature]);<br> <br> //add EditingToolbar<br> map.addControl(new OpenLayers.Control.EditingToolbar(pointLayer));<br>
<br> //attach to events<br> pointLayer.events.on({<br> "sketchcomplete": OpenLayers.Function.bind(report_1, this)<br> });<br> //callback function<br>
function report_1(event) {<br> <br> var bounds = event.feature.geometry.getBounds();<br><br> var ll = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.left, bounds.bottom));<br>
var ur = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.right, bounds.top));<br> <br> ll = ll.transform(proj, map.getProjectionObject());<br> ur = ur.transform(proj, map.getProjectionObject());<br>
<br> alert(ll.lon + ", " + ll.lat + ", " + ur.lon + ", " + ur.lat);<br> }<br> }<br> </script><br> </head><br> <body onload="init()"><br>
<div id="map" style="width:500px; height:500px;"></div><br> <div id="coordinates"></div><br> </body><br></html><br>