Movatterモバイル変換


[0]ホーム

URL:


Jump to content
MediaWiki
Search

API:Geosearch

From mediawiki.org
Translate this page
Languages:
This page is part of theMediaWiki 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 theExtension:GeoData currently not installed on MediaWiki but Wikipedia. So, in this document, we will use the URLen.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 ofWikimedia Foundation headquarters by providing the article title:

api.php?action=query&prop=coordinates&titles=Wikimedia%20Foundation [try in ApiSandbox]

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"""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

[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.jsMediaWiki API DemosDemo of `Geosearch` module: Obtain coordinates for wiki pages nearbyMIT 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 nearWikimedia 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"""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

[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.jsMediaWiki API DemosDemo of `Geosearch` module: Search for wiki pages nearbyMIT 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 theGenerator module to get search results for pages nearWikimedia Foundation headquarters with images. Parameters passed along with a generator must be prefixed with ag. Note that in the query below, we've changedgs coord toggs 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 moduleto get search results for pages near Wikimedia HQwith 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

[edit]
<?php/*    geoimagesearch.php    MediaWiki API Demos    Demo of `Geosearch` module: Use generator moduleto get search results for pages near Wikimedia HQwith 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 moduleto get search results for pages near Wikimedia HQwith 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.jsMediaWiki API DemosDemo of `Geosearch` module: Use generator moduleto get search results for pages near Wikimedia HQwith imagesMIT 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 ofAndroid andiOS app
  • Nearby is an app for thePebble smart watch that fetches Wikipedia articles near you.

Possible errors

[edit]
CodeInfo
badcoordInvalid coordinate provided

Additional notes

[edit]
  • This module is supported through theExtension:GeoData, currently installed on Wikimedia Commons, all Wikipedias, all Wikivoyage sites, and some other wikis. You can useSpecial: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-innavigator.geolocation object. SeeMDN docs.
    • If you want the user to provide coordinates, there are various services that let the user pick from a map. Seew:Wikipedia:Obtaining geographic coordinates.

See also

[edit]
Retrieved from "https://www.mediawiki.org/w/index.php?title=API:Geosearch&oldid=7800348"
Categories:

[8]ページ先頭

©2009-2026 Movatter.jp