🍎

Apple Maps Server APIを利用する

2022/12/25に公開

使い方わからなかったのでメモ

準備

1. MapIDの作成

https://developer.apple.com/account/resources/identifiers/list

Identifierを+から作成.

Certificates, Identifier

MapIDを選択
Map IDs

MapIDを登録
Register a MapID

2. Private keyの作成

https://developer.apple.com/account/resources/authkeys/list

Keysから生成
keys

名前を入力して MapKitJSをクリック. ConfigureでMapIDを選択
MapKitJS

登録
Register a New Key

DLする (二度と再度DLできないのでバックアップしておく)
Download Your Key

下記のような表示が出るので、KeyIDはメモっておく

Name:xxxx
Key ID:xxxx
Services:MapKit JS

3. JWTの作成

https://maps.developer.apple.com/token-maker

Create a Token

  • AppleDeveloperTeamID: 画面右上に表示されているやつ
  • MapKit JS Key ID: 先程メモったID
  • Domain restrictions: 適当に入力

Get Tokenボタンを押して生成されたJWTをメモっておく.

4. access tokenの生成

MAPKIT_TOKENに先程のTOKENを入れてaccessTokenをリクエスト.

$ export MAPKIT_JWT="" #先程のJWT
$ curl -si -H "Authorization: Bearer $MAPKIT_JWT" "https://maps-api.apple.com/v1/token"
{
  "accessToken": "xxx",
  "expiresInSeconds": 1800
}

得られたaccessTokenを利用してAPIリクエストする

APIリクエスト

検索

https://developer.apple.com/documentation/applemapsserverapi/search_for_places_that_match_specific_criteria

$ export MAPKIT_ACCESS_TOKEN="" #先程のaccessToken
$ curl -s -H "Authorization: Bearer $MAPKIT_ACCESS_TOKEN" "https://maps-api.apple.com/v1/search?q=eiffel%20tower" | jq .
{
  "displayMapRegion": {
    "southLatitude": 48.85524651501328,
    "westLongitude": 2.2895093075931072,
    "northLatitude": 48.863189332187176,
    "eastLongitude": 2.301520323380828
  },
  "results": [
    {
      "coordinate": {
        "latitude": 48.8582637,
        "longitude": 2.2942401
      },
      "displayMapRegion": {
        "southLatitude": 48.8565985,
        "westLongitude": 2.2915573,
        "northLatitude": 48.8618374,
        "eastLongitude": 2.2994722
      },
      "name": "Eiffel Tower",
      "formattedAddressLines": [
        "5 Avenue Anatole France",
        "75007 Paris",
        "France"
      ],
      "structuredAddress": {
        "administrativeArea": "Île-de-France",
        "locality": "Paris",
        "postCode": "75007",
        "subLocality": "7th Arr.",
        "thoroughfare": "Avenue Anatole France",
        "subThoroughfare": "5",
        "fullThoroughfare": "5 Avenue Anatole France",
        "areasOfInterest": [
          "Eiffel Tower",
          "Parc du Champ de Mars"
        ],
        "dependentLocalities": [
          "Tour Eiffel-Champs de Mars"
        ]
      },
      "country": "France",
      "countryCode": "FR"
    }
  ]
}

無事にリクエストできた!

Map Server API Playgroundを使うと簡単にリクエスト実験できる

制限

デフォルトだとチーム単位で 25,000 [calls/day] しか呼び出せないので注意.

The service provides up to 25,000 service calls per day per team between Apple Maps Server API and MapKit JS. If your app exceeds this quota, the service returns an HTTP 429 error (Too Many Requests) and your app needs to retry later. If your app requires a larger daily quota, submit a quota increase request form.

参考

Apple Maps Server API

MapIDとPrivateKeyの作成

Json Web Tokenの作成ツール

MapAccessTokenの作成

Playground

Discussion