API:Geosearch
Appearance
From mediawiki.org
This page is part of the MediaWiki Action API documentation.
| MediaWiki Action API |
|---|
| Basics |
| Authentication |
| Accounts and Users |
| Page Operations |
|
| Search |
| Developer Utilities |
| Tutorials |
| v · d · e |
GET request to search for wiki pages near a location with geographic coordinates or page name.
This module is supported through the Extension:GeoData currently not installed on MediaWiki but Wikipedia. So, in this document, we will use the URL
en.wikipedia.org in all API endpoints.API documentation
[edit ]View the complete documentation and list of supported parameters here.
Example 1: Obtain coordinates
[edit ]GET request
[edit ]Obtain coordinates of Wikimedia Foundation headquarters by providing the article title:
Response
[edit ]{ "batchcomplete":"", "query":{ "pages":{ "18618509":{ "pageid":18618509, "ns":0, "title":"Wikimedia Foundation", "coordinates":[ { "lat":37.7891838, "lon":-122.4033522, "primary":"", "globe":"earth" } ] } } } }
Sample code
[edit ]Python
[edit ]#!/usr/bin/python3 """ geocoordinates.py MediaWiki API Demos Demo of Geosearch module: Obtain coordinates for wiki pages nearby MIT License """ importrequests S = requests.Session() URL = "https://en.wikipedia.org/w/api.php" PARAMS = { "action": "query", "format": "json", "titles": "Wikimedia Foundation", "prop": "coordinates" } R = S.get(url=URL, params=PARAMS) DATA = R.json() PAGES = DATA['query']['pages'] for k, v in PAGES.items(): print("Latitute: " + str(v['coordinates'][0]['lat'])) print("Longitude: " + str(v['coordinates'][0]['lon']))
PHP
[edit ]<?php /* geocoordinates.php MediaWiki API Demos Demo of `Geosearch` module: Obtain coordinates for wiki pages nearby MIT License */ $endPoint = "https://en.wikipedia.org/w/api.php"; $params = [ "action" => "query", "prop" => "coordinates", "titles" => "Wikimedia Foundation", "format" => "json" ]; $url = $endPoint . "?" . http_build_query( $params ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $output, true ); foreach( $result["query"]["pages"] as $k => $v ) { echo("Latitute: " . $v["coordinates"][0]["lat"] . "\n" ); echo("Longitude: " . $v["coordinates"][0]["lon"] . "\n" ); }
JavaScript
[edit ]/* geocoordinates.js MediaWiki API Demos Demo of `Geosearch` module: Obtain coordinates for wiki pages nearby MIT License */ varurl="https://en.wikipedia.org/w/api.php"; varparams={ action:"query", prop:"coordinates", titles:"Wikimedia Foundation", format:"json" }; url=url+"?origin=*"; Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];}); fetch(url) .then(function(response){returnresponse.json();}) .then(function(response){ varpages=response.query.pages; for(varpageinpages){ console.log("Latitute: "+pages[page].coordinates[0].lat); console.log("Longitude: "+pages[page].coordinates[0].lon); } }) .catch(function(error){console.log(error);});
MediaWiki JS
[edit ]/* geocoordinates.js MediaWiki API Demos Demo of `Geosearch` module: Obtain coordinates for wiki pages nearby MIT License */ varparams={ action:'query', prop:'coordinates', titles:'Wikimedia Foundation', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ varpages=data.query.pages, page; for(pageinpages){ console.log('Latitute: '+pages[page].coordinates[0].lat); console.log('Longitude: '+pages[page].coordinates[0].lon); } });
Example 2: Search for pages nearby
[edit ]GET request
[edit ]Search for pages near Wikimedia Foundation headquarters by specifying the geographic coordinates of its location:
api.php? action=query& list=geosearch& gscoord=37.7891838|-122.4033522& gsradius=10000& gslimit=100 [try in ApiSandbox]
Response
[edit ]| Response |
|---|
{ "batchcomplete":"", "query":{ "geosearch":[ { "pageid":18618509, "ns":0, "title":"Wikimedia Foundation", "lat":37.7891838, "lon":-122.4033522, "dist":0, "primary":"" }, { "pageid":42936625, "ns":0, "title":"Foxcroft Building", "lat":37.789166666667, "lon":-122.40333333333, "dist":2.5, "primary":"" } ... ] } } |
Sample code
[edit ]| geosearch.py |
|---|
Python[edit ]#!/usr/bin/python3 """ geosearch.py MediaWiki API Demos Demo of `Geosearch` module: Search for wiki pages nearby MIT License """ importrequests S = requests.Session() URL = "https://en.wikipedia.org/w/api.php" PARAMS = { "format": "json", "list": "geosearch", "gscoord": "37.7891838|-122.4033522", "gslimit": "10", "gsradius": "10000", "action": "query" } R = S.get(url=URL, params=PARAMS) DATA = R.json() PLACES = DATA['query']['geosearch'] for place in PLACES: print(place['title']) PHP[edit ]<?php /* geosearch.php MediaWiki API Demos Demo of `Geosearch` module: Search for wiki pages nearby MIT License */ $endPoint = "https://en.wikipedia.org/w/api.php"; $params = [ "action" => "query", "list" => "geosearch", "gscoord" => "37.7891838|-122.4033522", "gsradius" => "10000", "gslimit" => "10", "format" => "json" ]; $url = $endPoint . "?" . http_build_query( $params ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $output, true ); foreach( $result["query"]["geosearch"] as $place ){ echo( $place["title"] . "\n" ); } JavaScript[edit ]/* geosearch.js MediaWiki API Demos Demo of `Geosearch` module: Search for wiki pages nearby MIT License */ varurl="https://en.wikipedia.org/w/api.php"; varparams={ action:"query", list:"geosearch", gscoord:"37.7891838|-122.4033522", gsradius:"10000", gslimit:"10", format:"json" }; url=url+"?origin=*"; Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];}); fetch(url) .then(function(response){returnresponse.json();}) .then(function(response){ varpages=response.query.geosearch; for(varplaceinpages){ console.log(pages[place].title); } }) .catch(function(error){console.log(error);}); MediaWiki JS[edit ]/* geosearch.js MediaWiki API Demos Demo of `Geosearch` module: Search for wiki pages nearby MIT License */ varparams={ action:'query', list:'geosearch', gscoord:'37.7891838|-122.4033522', gsradius:'10000', gslimit:'10', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ varpages=data.query.geosearch, place; for(placeinpages){ console.log(pages[place].title); } }); |
Example 3: Search for pages nearby with images
[edit ]GET request
[edit ]As an enhancement to Example 2, here we use the Generator module to get search results for pages near Wikimedia Foundation headquarters with images. Parameters passed along with a generator must be prefixed with a g. Note that in the query below, we've changed gs coord to ggs coord.
api.php? action=query& generator=geosearch& prop=coordinates|pageimages& ggscoord=37.7891838|-122.4033522 [try in ApiSandbox]
Response
[edit ]| Response |
|---|
{ "batchcomplete":"", "query":{ "pages":{ "2608926":{ "pageid":2608926, "ns":0, "title":"San Francisco Mechanics' Institute", "index":0, "coordinates":[ { "lat":37.788844, "lon":-122.403042, "primary":"", "globe":"earth" } ], "thumbnail":{ "source":"https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/MechanicsInstituteSanFrancisco.jpg/32px-MechanicsInstituteSanFrancisco.jpg", "width":32, "height":50 }, "pageimage":"MechanicsInstituteSanFrancisco.jpg" }, } } |
Sample code
[edit ]| geoimagesearch.py |
|---|
Python[edit ]#!/usr/bin/python3 """ geoimagesearch.py MediaWiki API Demos Demo of `Geosearch` module: Use generator module to get search results for pages near Wikimedia HQ with images MIT License """ importrequests S = requests.Session() URL = "https://en.wikipedia.org/w/api.php" PARAMS = { "action": "query", "format": "json", "ggscoord": "37.7891838|-122.4033522", "generator": "geosearch", "prop": "coordinates|pageimages" } R = S.get(url=URL, params=PARAMS) DATA = R.json() PLACES = DATA['query']['pages'] for k, v in PLACES.items(): print(str(v['title']) + ": " + str(v['thumbnail']['source'])) PHP[edit ]<?php /* geoimagesearch.php MediaWiki API Demos Demo of `Geosearch` module: Use generator module to get search results for pages near Wikimedia HQ with images MIT License */ $endPoint = "https://en.wikipedia.org/w/api.php"; $params = [ "action" => "query", "generator" => "geosearch", "prop" => "coordinates|pageimages", "ggscoord" => "37.7891838|-122.4033522", "format" => "json" ]; $url = $endPoint . "?" . http_build_query( $params ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); $output = curl_exec( $ch ); curl_close( $ch ); $result = json_decode( $output, true ); foreach( $result["query"]["pages"] as $k => $v ) { echo( $v["title"] . ": " . $v["thumbnail"]["source"] . "\n" ); } JavaScript[edit ]/* geoimagesearch.js MediaWiki API Demos Demo of `Geosearch` module: Use generator module to get search results for pages near Wikimedia HQ with images MIT License */ varurl="https://en.wikipedia.org/w/api.php"; varparams={ action:"query", generator:"geosearch", prop:"coordinates|pageimages", ggscoord:"37.7891838|-122.4033522", format:"json" }; url=url+"?origin=*"; Object.keys(params).forEach(function(key){url+="&"+key+"="+params[key];}); fetch(url) .then(function(response){returnresponse.json();}) .then(function(response){ varpages=response.query.pages; for(varpageinpages){ console.log(pages[page].title+": "+pages[page].thumbnail.source); } }) .catch(function(error){console.log(error);}); MediaWiki JS[edit ]/* geoimagesearch.js MediaWiki API Demos Demo of `Geosearch` module: Use generator module to get search results for pages near Wikimedia HQ with images MIT License */ varparams={ action:'query', generator:'geosearch', prop:'coordinates|pageimages', ggscoord:'37.7891838|-122.4033522', format:'json' }, api=newmw.Api(); api.get(params).done(function(data){ varpages=data.query.pages, page; for(pageinpages){ console.log(pages[page].title+': '+pages[page].thumbnail.source); } }); |
Demo app(s)
[edit ]- Special:Nearby on English Wikipedia shows articles of places around you
Screenshot of Wikipedia iOS app - shows places around Wikimedia Foundation HQ - Wikipedia Mobile Apps use this API to show nearby locations. API usage can be seen in the source code of Android and iOS app
- Nearby is an app for the Pebble smart watch that fetches Wikipedia articles near you.
Possible errors
[edit ]| Code | Info |
|---|---|
| badcoord | Invalid coordinate provided |
Additional notes
[edit ]- This module is supported through the Extension:GeoData , currently installed on Wikimedia Commons, all Wikipedias, all Wikivoyage sites, and some other wikis. You can use Special:Version of a wiki to check if the extension is listed there.
- In addition to using the API as to ask for a page coordinates (as explained in Example 1), here are a few more ways to obtaining them:
- If you want your user's current location, it's available through many OS-specific APIs. Recent browsers have an opt-in
navigator.geolocationobject. See MDN docs. - If you want the user to provide coordinates, there are various services that let the user pick from a map. See w:Wikipedia:Obtaining geographic coordinates.
- If you want your user's current location, it's available through many OS-specific APIs. Recent browsers have an opt-in