Ali Bahrami
Ali Bahrami

Reputation: 6073

Trouble to set coordinates in OpenLayers when using Long and Lat from GoogleMaps

Here is the coordinates of Tehran on Google Maps. https://www.google.com/maps/place/Tehran/@35.6964895,51.0696315,10z/data=!3m1!4b1!4m2!3m1!1s0x3f8e00491ff3dcd9:0xf0b3697c567024bc

35.6961° N, 51.4231° E

I'm trying to find this coordinate in OpenLayers but I had no luck, here is my code:

map = new ol.Map({
    target: 'sample-map',
    layers: [
        new ol.layer.Tile({
            source: new ol.source.OSM()
        })
    ],
    view: new ol.View({         
        center:  [35.6961, 51.4231],
        zoom: 4
    })
});

I'm also trying to get current coordinates in degrees but I don't know how.

Upvotes: 0

Views: 1665

Answers (1)

steenhulthin
steenhulthin

Reputation: 4773

I'm not entirely sure what is the problem, but I can say this: you need to swap your parameters on center to [51.4231, 35.6961].

According to the Openlayers documentation the center is in the format [x-axis, y-axis] or in your case [East, North].

In your specific case your source projection is NOT lat/long so you have to convert. The following code should work for you:

map = new ol.Map({ target: 'sample-map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() })], view: new ol.View({
center: ol.proj.transform([51.4231, 35.6961], 'EPSG:4326', new ol.source.OSM().getProjection()), zoom: 10 }) });

Upvotes: 2

Related Questions