[OpenLayers-Users] EditingToolbar with Google Base layer

Filipe Silva filipesilva1 at gmail.com
Mon Jul 11 14:37:34 EDT 2011


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>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110711/53217382/attachment-0001.html


More information about the Users mailing list