API:Nearby places viewer

Introduction
This shows how to query the MediaWiki API for nearby articles.



How it works in MediaWiki
The GeoData extension provides a query list submodule for. it is installed on commons, all Wikipedias, all Wikivoyage sites, and some other wikis. Use Special:Version to check. This finds wiki pages with associated geographic coordinates "near" a location. You can either give the geographic coordinates of a location, or a wiki page name that has its own geographic coordinates. The mobile view on those wikis exposes this capability as "Nearby" in the Wikipedia menu, as shown in the screenshot.

Let's perform a geosearch for pages near the Wikimedia Foundation offices

https://en.wikipedia.org/w/api.php?action=query&list=geosearch&format=json&gscoord=37.786952%7C-122.399523&gsradius=10000&gslimit=10

With just the coordinates and page name you can present a list of pages nearby.

Getting additional information in one query request
Page names alone are ambiguous and perhaps not exciting. Just as with search results, you could query each page returned for a pageimage and a description. Also just as with search results, you generally get better performance if you get all the information you want to display in a single query. Since a list query can also act as a Generator, you can do this: specify. The list of pages from the generator become the set of pages for the other part of the query, all in a single API request.

Note we change the geosearch zzz parameters to  zzz, since they are now parameters to the generator "geosearch".

desc=use geosearch as a generator, and ask for page description and thumbnail

https://en.wikipedia.org/w/api.php?action=query&prop=coordinates|pageimages|pageterms&format=json&colimit=50&piprop=thumbnail&pithumbsize=144&pilimit=50&wbptterms=description&generator=geosearch&ggscoord=37.786952%7C-122.399523&ggsradius=10000&ggslimit=50

How to get geographic coordinates
If you want your user's current location, it's available through many OS-specific APIs. Recent browsers have an opt-in  object (see MDN docs).

If you want the user to provide coordinates, there are various services that let the user pick from a map, see en:Wikipedia:Obtaining geographic coordinates.

If there's a page describing a feature nearby, you can ask that page for its coordinates, if any. In the case of en:Wikimedia Foundation, some editor added  markup to the page. This markup both displays the coordinates (and on English Wikipedia the MWikiMiniAtlas JavaScript library turns them into a clickable, draggable map) and causes Extension:GeoData's. You can programmatically ask for a page's coordinates using Extension:Geodata's other API, a page property query submodule :

https://en.wikipedia.org/w/api.php?action=query&prop=coordinates&titles=Wikimedia%20Foundation

Going beyond
The Wikipedia mobile apps show a compass pointing at the nearby locations. To do this you need to obtain the orientation of the device.



Next steps
Try these API requests in the Special:ApiSandbox page, then make the same API requests from your own applications.

Try mashing up OpenStreetMaps with these locations.

Make a game where you have to come up with a wiki page name as close to a given page as possible.