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.
#!/usr/bin/python3""" geocoordinates.py MediaWiki API Demos Demo of Geosearch module: Obtain coordinates for wiki pages nearby MIT License"""importrequestsS=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']fork,vinPAGES.items():print("Latitute: "+str(v['coordinates'][0]['lat']))print("Longitude: "+str(v['coordinates'][0]['lon']))
PHP
<?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
/* 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
/* 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
GET request
Search for pages near Wikimedia Foundation headquarters by specifying the geographic coordinates of its location:
#!/usr/bin/python3""" geosearch.py MediaWiki API Demos Demo of `Geosearch` module: Search for wiki pages nearby MIT License"""importrequestsS=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']forplaceinPLACES:print(place['title'])
PHP
<?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
/* 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
/* 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
GET request
Gẹgẹbi imudara si Apeere 2, nibi a lo Module Olupilẹṣẹ lati gba awọn abajade wiwa fun awọn oju-iwe nitosi Wikimedia Foundation pẹlu awọn aworan. Awọn paramita ti o kọja pẹlu monomono gbọdọ jẹ asọtẹlẹ pẹlu $3 kan. Ṣe akiyesi pe ninu ibeere ti o wa ni isalẹ, a ti yipada $4 si coord $5.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.
{"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
geoimagesearch.py
Python
#!/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"""importrequestsS=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']fork,vinPLACES.items():print(str(v['title'])+": "+str(v['thumbnail']['source']))
PHP
<?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
/* 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
/* 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)
Special:Nearby on English Wikipedia shows articles of places around youScreenshot 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
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.geolocation object.See MDN docs.