0

How to get Map corner latlong using open layer.??? i had tried with map.getBounds() method but it shows error like "map.getBounds is not a function"

so kindly reply for this error or any example.

here i had use OpenLayers.js

asked 07 Jul '16, 08:44

Jatin%20Patel's gravatar image

Jatin Patel
11557
accept rate: 0%

edited 07 Jul '16, 09:24

scai's gravatar image

scai ♦
31.9k20291442

You have to provide more information. Show us the part of your code where getBounds() is failing.

(11 Jul '16, 08:25) scai ♦

3 Answers:
0

getBounds() only works for OpenLayers 2. Since it is failing for you I assume that you are using OpenLayers 3.

For OpenLayers 3 you have to look at the map extent by calling map.getView().calculateExtent() according to these similar questions:

permanent link

answered 07 Jul '16, 09:24

scai's gravatar image

scai ♦
31.9k20291442
accept rate: 23%

map.getView().calculateExtent() does not work in OpenLayer3.

i use below link in open layer.

http://www.openlayers.org/api/OpenLayers.js

(11 Jul '16, 08:15) Jatin Patel
1

This is OpenLayers 2.

(11 Jul '16, 08:24) scai ♦
1

Your GetData() function has no local map object. You need to pass your map object as parameter or make it global. Currently it is only known inside init().

(12 Jul '16, 10:56) scai ♦
0

Above Both function not working.

below is the my code, please correct it.

=====================================

<html> <head> <title>OpenLayers Demo</title> <style type="text/css"> html, body, #basicMap { width: 100%; height: 100%; margin: 0; } </style>

<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript"></script>

<script type="text/javascript">
    function init() {
        var marker_layer = new OpenLayers.Layer.Markers("Markers");

        map = new OpenLayers.Map("basicMap");
        var mapnik = new OpenLayers.Layer.OSM();
        var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
        var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection

        var position1 = new OpenLayers.LonLat(78.9629, 20.5937).transform(fromProjection, toProjection);
        var position2 = new OpenLayers.LonLat(79.2629, 20.9937).transform(fromProjection, toProjection);
        var zoom = 8;

        map.addLayer(mapnik);
        map.setCenter(position1, zoom);

    }
</script>

<script type="text/javascript">
    function GetData() {
        debugger;

        //var bounds = map.getBounds();       //Error : map.getBounds is not a function

        //var vv1 = map.getView().calculateExtent();  //Error : map.getView is not a function
    }
</script>

</head> <body onload="init();"> <input id="Button1" type="button" value="Click Here" onclick="GetData();"/>

</body> </html>

=====================================

permanent link

answered 12 Jul '16, 10:39

Jatin%20Patel's gravatar image

Jatin Patel
11557
accept rate: 0%

edited 12 Jul '16, 10:40

0

At this i have not use function but still i got an error.

==============================================

<html> <head> <title>OpenLayers Demo</title> <style type="text/css"> html, body, #basicMap { width: 100%; height: 100%; margin: 0; } </style>

<script src="http://www.openlayers.org/api/OpenLayers.js" type="text/javascript"></script>

<script type="text/javascript">
    function init() {
        var marker_layer = new OpenLayers.Layer.Markers("Markers");

        var map = new OpenLayers.Map("basicMap");
        var mapnik = new OpenLayers.Layer.OSM();
        var fromProjection = new OpenLayers.Projection("EPSG:4326");   // Transform from WGS 1984
        var toProjection = new OpenLayers.Projection("EPSG:900913"); // to Spherical Mercator Projection

        var position1 = new OpenLayers.LonLat(78.9629, 20.5937).transform(fromProjection, toProjection);
        var position2 = new OpenLayers.LonLat(79.2629, 20.9937).transform(fromProjection, toProjection);
        var zoom = 8;

        map.addLayer(mapnik);
        map.setCenter(position1, zoom);


        debugger;

        var bounds = map.getBounds();       //Error : map.getBounds is not a function

        var vv1 = map.getView().calculateExtent();  //Error : map.getView is not a function

    }
</script>

</head> <body onload="init();">

</body> </html>

==============================================

permanent link

answered 12 Jul '16, 12:32

Jatin%20Patel's gravatar image

Jatin Patel
11557
accept rate: 0%

Try map.getExtent() for OpenLayers 2, see http://dev.openlayers.org/docs/files/OpenLayers/Map-js.html#OpenLayers.Map.getExtent. And please don't add answers to your question, use the comment function instead.

(16 Jul '16, 07:47) scai ♦

Then what was the formula for find corner latlong.????

(19 Jul '16, 10:34) Jatin Patel
2

getExtent() returns an OpenLayers.Bounds object. It has left, bottom, right and top as properties.

(19 Jul '16, 10:45) scai ♦

Is there any calculation for getting corner latlong from the getExtend(). because i got only one value from left, bottom, right and top. so kindly help me.

(21 Jul '16, 06:48) Jatin Patel

Please take some time and read through the documentation. See OpenLayers.LonLat, just pass the corresponding corners in the constructor.

(21 Jul '16, 07:38) scai ♦

Thanks a lot sir... i got solution for my question but now i want to set view using this bounds left, bottom, right,top and zoom. so how can i do this.???

(27 Jul '16, 07:22) Jatin Patel
showing 5 of 6 show 1 more comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×117
×2

question asked: 07 Jul '16, 08:44

question was seen: 5,644 times

last updated: 27 Jul '16, 07:23

powered by OSQA