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

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 can query the page names returned for a pageimage and a description. Also just as with search results, you generally get better performance by making fewer queries; ideally you make a single API request to get all the information you want to display. 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".

How to get geographic coordinates
The queries above hardcode the coordinates (37.786952, -122.399523). In practice you either want to show something near your user, or near some other known item.

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, that page may provide coordinates for the feature. In the case of the en:Eiffel Tower, 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 GeoData's to be called. You can programmatically ask for a page's coordinates using Geodata's other API, a page property query submodule :

If you want to show items near an article, you can simply provide the article title to the geosearch as a  parameter instead of geographic coordinates  in the    parameter:

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.



Other languages and Wikidata
This works well in English because English Wikipedia has many articles that have a coordinates property. Other languages may have far fewer articles with coordinates.

The ultimate resolution of this is to store coordinates as properties of language-independent items in Wikidata. Then you can present items nearby in any language. If you follow the "Wikidata item" link in the sidebar of English Wikipedia's 140 New Montgomery Street, you can see that Q14681501 does have a "coordinate location" (property P625), which turns out to be imported from this Wikipedia article. the tools for geographic querying of Wikidata items are in flux:
 * the experimental WikidataQuery API has an  command that locates properties with geographic proximity (in kilometers). So to ask for items with a "coordinate location" property (P625) within 10 km of the same coordinates used in the above example, you can request https://wdq.wmflabs.org/wdq/?q=around%5B625,37.78697,-122.399677.119,10%5D
 * the Wikidata query service have geographical queries too.

Next steps
Try these API requests in the Special:ApiSandbox, 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.