Google Play 開発者サービスの Location API / Context API に移行する
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
位置情報サービスを必要とするアプリでは、Google Play 開発者サービスのLocation API / Context API を使用することをおすすめします。Framework Location API を使用しているアプリの場合は、最新の Google 機能を活用できるように、Google Play 開発者サービスに移行することが重要です。
アプリで位置情報サービスを利用する場合は、Google Play 開発者サービスの使用をおすすめします。その理由は以下のとおりです。
- Google Play 開発者サービスは、シンプルなインターフェースと洗練された API サーフェスを備えています。
- 望ましいサービス品質(QoS)を指定すると、API が自動的に基盤テクノロジーを管理します。
- Google Play Services API は、パフォーマンスと電池消費の両面で最適化されています。
- Google Play Services API は、積極的にメンテナンスが行われています。Google は、アルゴリズムの改善と機能の追加を継続的に推進しています。
アプリを更新する
Location API / Context API を使用するようにアプリを更新する手順は次のとおりです。
- プロジェクト内で Google Play 開発者サービスをセットアップします。
- Location Settings API を使用して、現在の位置情報の設定を検証します。
- Google Play 開発者サービスは、ジオフェンス、操作の認識、認知度などの複雑な機能に使用します。
- Framework Location API を使用している場合は、Fused Location Provider API に置き換えます。
- Framework Location API への参照を削除します。
プロジェクト内で Google Play 開発者サービスをセットアップする
プロジェクトで Loction API / Context API を利用できるようにするには、Google Maven リポジトリへの参照を追加して、必要な API に対する依存関係を宣言する必要があります。詳細については、Google Play 開発者サービスをセットアップするをご覧ください。
Location Settings API を使用する
Location Settings API を使用すれば、アプリで望ましい QoS レベルを提供できます。また、システム設定の必要な変更をユーザーにリクエストする際も API に任せられます。アプリで Location Settings API を使用する手順は次のとおりです。
- アプリ マニフェストで位置情報の利用許可をリクエストします。
- 望ましい QoS レベルを指定する
LocationRequest
オブジェクトをセットアップします。
- Location Settings API を使用して、現在の設定を確認します。
詳細については、位置情報の設定を変更するをご覧ください。サンプルコードについては、Google Play Location サンプルをご覧ください。
複雑な機能に Google Play 開発者サービスを使用する
Google Play 開発者サービス ライブラリは、ユーザーの周囲のコンテキストと認知度に関する新しいユーザー エクスペリエンスの実装に役立ちます。各種の位置情報とコンテキスト ライブラリは、位置情報以外にも追加のセンサーを利用し、電力効率の良い方法で、バッテリー駆動時間への影響を軽減しながらアプリにより高精度なデータを提供します。
独自のカスタム ソリューションを作成する代わりに、これらのライブラリをアプリに組み込みます。
Framework Location API を Fused Location Provider API に置き換える
緯度と経度などの位置情報データを取得するには、Fused Location Provider API を使用できます。Framework Location API と同様に、Fused Location Provider API は Location
オブジェクトを使用して地理的位置を表します。この API は、位置情報の更新をリッスンする機能や、直近の位置情報を取得する機能を備えています。Fused Location Provider API はこうしたさまざまな機能を備えているので、アプリの他の部分に対する変更を最小限に抑えつつ、Framework Location API を使用するコンポーネントを置き換える候補として最適です。
直近の位置情報の取得は、デバイス上の任意のクライアントからリクエストされる位置情報データを使用する高速オペレーションであるため、多くのエクスペリエンスの良い出発点になります。定期的に位置情報をトラッキングする場合、アプリは位置情報の更新を定期的にリクエストできます。これにより、最新のデータを取得して、複雑なエクスペリエンスを提供することが可能になります。
Framework Location API への参照を削除する
com.google.android.location
パッケージ内のクラスへの参照を、com.google.android.gms.location
パッケージのクラスへの参照に置き換えます(ただし、Fused Location Provider API が使用する Location
クラスへの参照は除きます)。通常は、GPS や Wi-Fi などの各種プロバイダを管理するコンポーネントをアプリから削除できます。Location API / Context API は、それらのプロバイダを自動的に管理します。
アプリをテストする
最新バージョンの Google Play 開発者サービスを使用するアプリを実行するには、Play ストア アプリをインストールしたデバイスで Google アカウントにログインする必要があります。開発目的の場合は、以下のいずれかのオプションを使用できます。
- USB ケーブルを使用して開発環境に接続された物理デバイス。
- Play ストア アプリをインストールしたエミュレータ。
物理デバイスを開発環境に接続する方法については、ハードウェア デバイス上でアプリを実行するをご覧ください。Play ストア アプリをインストールしたエミュレータを作成する方法については、仮想デバイスを作成して管理するをご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-07-27 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],null,["# Migrate to Google Play services location and context APIs\n\nGoogle recommends using the [location and context\nAPIs](https://developers.google.com/location-context/) in Google Play services\nin apps that require location services. If your app uses the [framework location\nAPIs](/reference/android/location/package-summary) it's important to\nmigrate to Google Play services to take advantage of the latest Google-powered\nfeatures.\n\nUsing Google Play services is the preferred option to get location services in\nyour app for the following reasons:\n\n1. Google Play services provide a simple interface and a cleaner API surface.\n2. You specify a desired quality of service and the APIs manage the underlying technologies for you.\n3. The Google Play services APIs are optimized for performance and battery usage.\n4. The Google Play services APIs are actively maintained. Google is constantly improving the algorithms and adding more features.\n\nUpdate your app\n---------------\n\nThe following steps describe the process to update an app to use the location\nand context APIs:\n\n1. Set up Google Play services in your project.\n2. Use the location settings API to validate the current location settings.\n3. Use Google Play services for complex features like geofencing, activity recognition, and awareness.\n4. Replace usage of the framework location API with the fused location provider API.\n5. Remove references to the framework location API.\n\n### Set up Google Play services in your project\n\nTo make the location and context APIs available to your project you must add a\nreference to the Google maven repository and declare a dependency to the\nrequired APIs. For more information, see [Set Up Google Play\nservices](https://developers.google.com/android/guides/setup).\n\n### Use the location settings API\n\nBy using the location settings API, apps provide the desired QoS level and the\nAPI requests the user for the appropriate changes to the system settings. Take\nthe following steps to use the location settings API in your app:\n\n1. [Request location permissions](/training/location/permissions) in the app manifest.\n2. Set up a [`LocationRequest`](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest.html) object, which specifies the desired QoS level.\n3. Use the location settings API to check the current settings.\n\nFor more information, see [Changing Location\nSettings](/training/location/change-location-settings#location-request) or\nsee the [Google Play Location samples](https://github.com/android/platform-samples/tree/main/samples/location/src/main/java/com/example/platform/location)\nfor example code.\n\n### Use Google Play services for complex features\n\nGoogle Play services libraries help you implement novel user experiences around\ncontext and awareness of the user's surroundings. The various [location and\ncontext](https://developers.google.com/location-context/) libraries leverage\nadditional sensors beyond just location, and do so in a power-efficient way,\nproviding your app with more accurate data while reducing the impact on battery\nlife.\n\nIncorporate these libraries in your app instead of writing custom solutions of\nyour own:\n\n- [Geofencing API](https://developers.google.com/location-context/geofencing)\n- [Activity Recognition API](https://developers.google.com/location-context/activity-recognition)\n- [Google Awareness API](https://developers.google.com/awareness)\n\n### Replace the framework location API with the fused location provider API\n\nYou can use the fused location provider API to get location data, such as\nlatitude and longitude. The fused location provider API uses a [`Location`](/reference/android/location/Location) object---just like the location\nframework API---to represent geographic location. The API provides features\nto listen for location updates as well as to get the last known location. All\nthese features make the fused location provider API a good candidate to replace\nthe components that use the framework location API with minimal changes to the\nrest of the app.\n\n[Getting the last known location](/training/location/retrieve-current) is a\ngood starting point for many experiences because it's a fast operation that uses\nlocation data requested by any client on the device. To periodically track\nlocation, your app can subscribe to\n[receive location updates](/training/location/receive-location-updates),\nwhich provides up-to-date data and enables more complex experiences.\n\n### Remove references to the framework location API\n\nReplace references to classes in the `com.google.android.location` package with\nclasses from the `com.google.android.gms.location` package, except references to\nthe [`Location`](/reference/android/location/Location) class, which the\nfused location provider API uses. You can usually remove the components that\nmanage the different providers, such as GPS and Wi-Fi, from your app. The\nlocation and context APIs automatically manage these providers.\n\nTest your app\n-------------\n\nTo run an app that uses the latest version of Google Play services, you need a\ndevice that has the Play Store app installed and a Google Account must be signed\nin. For development purposes you can use the following options:\n\n- A physical device connected to your development environment using a USB cable.\n- An emulator with the Play Store app installed.\n\nFor more information about connecting a physical device to your development\nenvironment, see [Run Apps on a Hardware Device](/studio/run/device). To\ncreate an emulator that includes the Play Store app, see [Create and Manage\nVirtual Devices](/studio/run/managing-avds)."]]