Last updated on March 6, 2023 by Antti Koskenrouta

I think Wikipedia is great. In fact, I think it’s so great that Wikimedia Foundation was the recipient of my first ever real charitable donation. If I ever need to check a fact or learn more about anything, I tend to add “wiki” to my search query just to get Wikipedia page show up first in the results.

A couple of months back I started thinking how great it would be to be able to query Wikipedia based on your geolocation. The applications would be limitless; it would offer a completely new way for people to experience a new city, and I believe it would also lead to more entries and edits made by people. I thought it was a no-brainer since most, if not all, Wikipedia entries have the coordinates saved right there in the post. I was surprised to find out that wasn’t the case.

It wasn’t until last week when I found Wikilocation.org, a service that lets you query Wikipedia based on coordinates.

I got so excited about the API, I wanted to build a quick demo just to see how it works. Like with any project, there were a few hurdles but all in all it was a really straightforward process.

Surprisingly the biggest challenge was to get the ajax call working. First, when I spcified JSON as the data type in the jQuery call, I ran into problems with the same origin policy. Then when I tried setting the data type to JSON, the ajax call would work but I got a ‘missing label’ error from jQuery. This was because the call would go through (thanks to JSONP) but the response from the server would still be plain JSON and it didn’t have the wrapper.

The trick was that you had to specify JSON as the format, use the JSONP attribute to set the callback function name and append “callback=?” to the querystring to tell jQuery it was a JSONP call. Once I got the data from the server, the rest was pretty easy.

I chose to add simple markers to the map and attach info windows to each one. The windows show the title of the Wikipedia article and have a link to the page. I added a simple user agent sniffing function to the page to detect iPhones and iPods to send them to a mobile version of the page.

Wikilocation API allows you to set the radius from the location up to 5 kilometers (about 3.1 miles) but it won’t return more than 50 results per a call. They offer an offset parameter to implement pagination. They also allow connecing to Foursquare and Gowalla but those will have to wait.

Categories: Blog