NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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 ♦
33.3k21309459

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

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

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 ♦
33.3k21309459
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 ♦

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

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and 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:

×122
×2

question asked: 07 Jul '16, 08:44

question was seen: 9,489 times

last updated: 27 Jul '16, 07:23

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum