[OpenLayers-Users] EditingToolbar with Google Base layer

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Wed Jul 13 13:43:01 EDT 2011


Hi Filipe,

I don't know if this is happening with your application, but I had a similar
problem with data from the twitter feed which was being given to me as
latlon, rather than the lonlat that OL expects. Because the values in a
mercator projection were outside of what the reprojection function expected,
they produced a NaN for me on occasion. Take a look at how the data is being
returned from your click request before your reproject it...it's very
unlikely, but it maybe that the values being returned to you from the click
are in google's latlon...


On Mon, Jul 11, 2011 at 1:37 PM, Filipe Silva <filipesilva1 at gmail.com>wrote:

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


More information about the Users mailing list