0

I have developed an app using Openlayers 2 to retrieve the latitude and longitude values based on the clicked position.

The map display the area of Great Britain and Northern Ireland.

Below the code I wrote for that.

var lonlat = self._map.getLonLatFromViewPortPx(evt.xy);

However Lonlat value it return is Latitude : 515881.5 and Longitude : 172199

I'm not sure whether these values are correct or not. Because Google map gives a different LonLat values for same area. [link]

The values from google maps are Latitude : 51.438677 Longitude : -0.335115

I want to know how to retrieve the LatLon from openlayers 2 if this is wrong.

Edit : This is the object returning from Openlayers end

initialize
lat
:
256465
lon
:
385906.5
__proto__
:
Object
CLASS_NAME
:
"OpenLayers.LonLat"
add
:
(lon, lat)
arguments
:
null
caller
:
null
length
:
2
name
:
"add"
prototype
:
Object
constructor
:
(lon, lat)
arguments
:
null
caller
:
null
length
:
2
name
:
"add"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1867
[[Scopes]]
:
Scopes[1]
0
:
Global
__proto__
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1867
[[Scopes]]
:
Scopes[1]
clone
:
()
arguments
:
null
caller
:
null
length
:
0
name
:
"clone"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1852
[[Scopes]]
:
Scopes[1]
equals
:
(ll)
arguments
:
null
caller
:
null
length
:
1
name
:
"equals"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1887
[[Scopes]]
:
Scopes[1]
initialize
:
(lon, lat)
arguments
:
null
caller
:
null
fromArray
:
(arr)
fromString
:
(str)
length
:
2
name
:
"initialize"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1813
[[Scopes]]
:
Scopes[1]
lat
:
0
lon
:
0
toShortString
:
()
arguments
:
null
caller
:
null
length
:
0
name
:
"toShortString"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1841
[[Scopes]]
:
Scopes[1]
toString
:
()
arguments
:
null
caller
:
null
length
:
0
name
:
"toString"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1830
[[Scopes]]
:
Scopes[1]
transform
:
(source, dest)
arguments
:
null
caller
:
null
length
:
2
name
:
"transform"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1908
[[Scopes]]
:
Scopes[1]
0
:
Global
wrapDateLine
:
(maxExtent)
arguments
:
null
caller
:
null
length
:
1
name
:
"wrapDateLine"
prototype
:
Object
__proto__
:
()
[[FunctionLocation]]
:
OpenLayers.debug.js:1927
[[Scopes]]
:
Scopes[1]
__proto__
:
Object

Edit 2: These are my grid references "gridReference": "386170,256495",

Edit 3

projection: "EPSG:27700",
        units: "m",
        maxExtent: {
            left: -521611.7877215,
            bottom: -99060.2018315805,
            right: 1107515.6274664556,
            top: 1234593.14792
        },

        scales: [1.025694, 0.533333, 0.238194, 0.139583, 0.090278, 1.35, 1.17, 1.08, 1.04, 1.02],
        resolutions: [1,0.5,0.25,0.1,0.05,0.025,0.0125,0.00625,0.003125,0.0015625,0.00078125],

        controls: ["navigation", "panZoomBar", "scaleLine", "mousePosition", "attribution", "loadingPanel", "navigationHistory"],
        controlPanel: {
            controls: ["zoomBox", "measure", "getFeature"],
            defaultControl: "panPanel"
        }
8
  • 2
    It seems you get projected coordinates instead of geographic long&lat. Check if you define some projected reference system anywhere in your application.
    – nadya
    Commented Dec 29, 2016 at 16:13
  • @NadyaB. How can I check the reference system? Commented Dec 29, 2016 at 16:39
  • 1
    Please do not ever state you have a "urgent requirement" in in a volunteer environment like GIS SE. All it does is generate downvotes and encourages folks to ignore your question.
    – Vince
    Commented Dec 29, 2016 at 16:46
  • Show the code....
    – BradHards
    Commented Dec 30, 2016 at 5:03
  • @NadyaB. Please see the edit 2 Commented Dec 30, 2016 at 5:46

1 Answer 1

1

As Nadya B. pointed out, your coordinates are projected (in OSGB36, which is what the projection: "EPSG:27700" part means).

If you want your map to be in lon/lat, use EPSG:4326 or similar.

If you want to keep your projected coordinates map, and just get the results in a longitude / latitude, use proj4js or similar to convert the results. Examples are provided in the openlayers doc: http://dev.openlayers.org/examples/using-proj4js.html and http://dev.openlayers.org/examples/graticule.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.