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

I have a script that queries the front browser tab and if it's an OpenStreetMap.org map URL, extracts the latitude and longitude of the map's centre. This used to be accomplished simply by sending the following Javascript to the front tab:

map.getCenter();

However, I haven't used the script in some time and this no longer works. I believe OSM was still using OpenLayers when I last used the script successfully and it's now using Leaflet? In any case, I've spent a couple of hours trying to find the map variable that I can send the getCenter() function to as detailed in the Leaflet documentation, but no joy. Can anyone tell me how I can use Javascript to get the centre of the map on the current OSM website? To be clear, I'm not trying to write my own mapping application using OSM, I want this to work with the main OpenStreetMap.org website.

Thanks in advance.

asked 03 Sep '15, 19:48

Jolin%20Warren's gravatar image

Jolin Warren
11225
accept rate: 0%


Center lat/lon and zoom level are in the URL of where ever your www.openstreetmap.org window is looking. . . Its been a while since I've looked at Javascript but I think you can access the URL within your code and then parse out the stuff you want.

permanent link

answered 03 Sep '15, 20:30

n76's gravatar image

n76
10.8k1082172
accept rate: 17%

I did think about doing that, but the URL doesn't automatically update as you drag the map. One option is doing a page source dump and then searching through, as there are embedded links in the page that have updated lat/lon, but I'd rather not be screen scraping if I can avoid it. I'd prefer a cleaner solution where I query the lat/lon specifically. If it's possible!

(03 Sep '15, 20:43) Jolin Warren

Thanks, that helped. I don't know what I was doing previously, but it turns out that the latitude, longitude, and zoom are continuously updated in the URL, and that bookmarklet uses the URL (window.location.hash). This works wonderfully. Thanks for your help.

(04 Sep '15, 13:03) Jolin Warren

My mistake. I don't know what I was doing before, but you are right that the lat/lon and zoom are always updated in the URL (after the hash). Thanks stf, this is the way to go. You'll see from my answer below that I used the Javascript property window.location.hash to extract the info I needed. Many thanks.

(04 Sep '15, 13:04) Jolin Warren

There's a bookmarklet here that does it so I'm sure that you could get some info out of that :)

permanent link

answered 03 Sep '15, 21:00

SomeoneElse's gravatar image

SomeoneElse ♦
36.9k71370866
accept rate: 16%

Your answer
toggle preview

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:

×362
×74
×60

question asked: 03 Sep '15, 19:48

question was seen: 7,171 times

last updated: 04 Sep '15, 13:26

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