Skip to content

ジオコーディング

AstroAPIは、場所を検索して座標を取得するためのジオコーディングエンドポイントを提供します。これは、計算エンドポイントで必要な緯度・経度の値への場所名の変換に役立ちます。

場所の検索

名前で都市、町、その他の場所を検索する:

bash
curl -X GET "https://api.astroapi.cloud/api/geocoding/search?q=Amsterdam" \
  -H "X-Api-Key: your-api-key"

クエリパラメータ

パラメータ必須説明
qはい検索クエリ(2〜100文字)
limitいいえ最大結果数(1〜10、デフォルト:5)
langいいえ言語コード(デフォルト:"en")

レスポンス

json
{
  "data": [
    {
      "id": "node_123456",
      "name": "Amsterdam",
      "displayName": "Amsterdam, North Holland, Netherlands",
      "latitude": 52.3676,
      "longitude": 4.9041,
      "country": "Netherlands",
      "countryCode": "NL",
      "state": "North Holland",
      "city": "Amsterdam"
    }
  ]
}

レスポンスフィールド

フィールド説明
id一意の識別子(OpenStreetMap ID)
name主要な場所名
displayName地域と国を含む完全にフォーマットされた名前
latitude緯度座標
longitude経度座標
country国名
countryCodeISO 2文字の国コード
state州・県・地域名
city市名(該当する場合)

コードサンプル

JavaScript

javascript
async function searchPlace(query) {
  const response = await fetch(
    `https://api.astroapi.cloud/api/geocoding/search?q=${encodeURIComponent(query)}`,
    {
      headers: {
        "X-Api-Key": "your-api-key"
      }
    }
  );
  const { data } = await response.json();
  return data;
}

// 使用例
const results = await searchPlace("London");
const { latitude, longitude } = results[0];
console.log(`London: ${latitude}, ${longitude}`);

Python

python
import requests

def search_place(query):
    response = requests.get(
        "https://api.astroapi.cloud/api/geocoding/search",
        params={"q": query},
        headers={"X-Api-Key": "your-api-key"}
    )
    return response.json()["data"]

# 使用例
results = search_place("Paris")
place = results[0]
print(f"Paris: {place['latitude']}, {place['longitude']}")

ネイタルチャートとの組み合わせ

ジオコーディングとネイタルチャート計算を組み合わせる:

javascript
// ステップ1:生誕地を検索する
const places = await searchPlace("New York");
const birthplace = places[0];

// ステップ2:ネイタルチャートを計算する
const response = await fetch("https://api.astroapi.cloud/api/calc/natal", {
  method: "POST",
  headers: {
    "X-Api-Key": "your-api-key",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({
    datetime: "1990-06-15T14:30:00",
    latitude: birthplace.latitude,
    longitude: birthplace.longitude,
    timezone: "America/New_York"
  })
});

言語サポート

ジオコーディングエンドポイントは、場所名に複数の言語をサポートしています。lang パラメータを使用してください:

bash
# ドイツ語
curl "https://api.astroapi.cloud/api/geocoding/search?q=Munich&lang=de"

# フランス語
curl "https://api.astroapi.cloud/api/geocoding/search?q=Munich&lang=fr"

サポートされている言語コードには endefresitnlptrujazh などが含まれます。

キャッシュ

パフォーマンスを向上させるため、ジオコーディング結果は24時間キャッシュされます。同じ検索クエリは、上流のジオコーディングサービスにアクセスせずにキャッシュされた結果を返します。

レート制限

ジオコーディングリクエストはAPIレート制限にカウントされます。詳細は レート制限 を参照してください。

AstroAPI Documentation