API:Geosearch/ja

This page is a translated version of the page API:Geosearch and the translation is 61% complete.

付近の場所のウィキページを地理座標またはページ名によって検索するGET リクエストです。

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の説明文書

View the complete documentation and list of supported parameters here.

例1: 座標を取得

GET リクエスト

Obtain coordinates of Wikimedia Foundation headquarters by providing the article title:


レスポンス

{
    "batchcomplete": "",
    "query": {
        "pages": {
            "18618509": {
                "pageid": 18618509,
                "ns": 0,
                "title": "Wikimedia Foundation",
                "coordinates": [
                    {
                        "lat": 37.7891838,
                        "lon": -122.4033522,
                        "primary": "",
                        "globe": "earth"
                    }
                ]
            }
        }
    }
}

サンプル コード

Python

#!/usr/bin/python3

"""
    geocoordinates.py

    MediaWiki API Demos
    Demo of `Geosearch` module: Obtain coordinates for wiki pages nearby

    MIT License
"""

import requests

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

<?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
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    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){return response.json();})
    .then(function(response) {
        var pages = response.query.pages;
        for (var page in pages) {
            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
*/

var params = {
		action: 'query',
		prop: 'coordinates',
		titles: 'Wikimedia Foundation',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		page;
	for ( page in pages ) {
		console.log( 'Latitute: ' + pages[ page ].coordinates[ 0 ].lat );
		console.log( 'Longitude: ' + pages[ page ].coordinates[ 0 ].lon );
	}
} );

例2: ページに対して付近を検索

GET リクエスト

Search for pages near Wikimedia Foundation headquarters by specifying the geographic coordinates of its location:



レスポンス

レスポンス
{
    "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": ""
            }
            ...
        ]
    }
}

サンプル コード

geosearch.py

Python

#!/usr/bin/python3

"""
    geosearch.py

    MediaWiki API Demos
    Demo of `Geosearch` module: Search for wiki pages nearby

    MIT License
"""

import requests

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

<?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
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    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){return response.json();})
    .then(function(response) {
        var pages = response.query.geosearch;
        for (var place in pages) {
            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
*/

var params = {
		action: 'query',
		list: 'geosearch',
		gscoord: '37.7891838|-122.4033522',
		gsradius: '10000',
		gslimit: '10',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.geosearch,
		place;
	for ( place in pages ) {
		console.log( pages[ place ].title );
	}
} );

例3: 画像ありでページに対して付近を検索

GET リクエスト

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.



レスポンス

レスポンス
{
    "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"
            },
      }
}

サンプル コード

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

import requests

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

<?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
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    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){return response.json();})
    .then(function(response) {
        var pages = response.query.pages;
        for (var page in pages) {
            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
*/

var params = {
		action: 'query',
		generator: 'geosearch',
		prop: 'coordinates|pageimages',
		ggscoord: '37.7891838|-122.4033522',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		page;
	for ( page in pages ) {
		console.log( pages[ page ].title + ': ' + pages[ page ].thumbnail.source );
	}
} );

デモ アプリ

  • 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.

起こりうるエラー

コード 情報
badcoord Invalid coordinate provided

追加的な注記

  • 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.
    • 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.

関連項目