Android Location API can be used to track your mobile current location and show in the app. In this tutorial, we’ll develop an application that fetches the user’s current location programmatically.
Android Location API可用于跟踪您的移动设备当前位置并在应用程序中显示。 在本教程中,我们将开发一个应用程序,该程序以编程方式获取用户的当前位置。
Android Location API (Android Location API)
There are two ways to get a users location in our application:
有两种方法可以在应用程序中获取用户位置:
android.location.LocationListener
: This is a part of the Android API.android.location.LocationListener
:这是Android API的一部分。com.google.android.gms.location.LocationListener
: This is present in the Google Play Services API. (We’ll look into this in the next tutorial)com.google.android.gms.location.LocationListener
:它存在于Google Play服务API中。 (我们将在下一个教程中对此进行研究)
Android Location Services is available since Android API 1.
Google officially recommends using Google Play Location Service APIs. Android Location Services API is still used to develop location-based apps for devices that don’t support Google Play Services.
自Android API 1起即可使用Android定位服务。
Google正式建议使用Google Play定位服务API。 Android Location Services API仍用于为不支持Google Play服务的设备开发基于位置的应用。
LocationListener (LocationListener)
The LocationListener interface, which is part of the Android Locations API is used for receiving notifications from the LocationManager when the location has changed. The LocationManager class provides access to the systems location services.
LocationListener接口是Android Locations API的一部分,用于在位置更改后从LocationManager接收通知。 LocationManager类提供对系统位置服务的访问。
The LocationListener class needs to implement the following methods.
LocationListener类需要实现以下方法。
- onLocationChanged(Location location) : Called when the location has changed. onLocationChanged(位置位置) :位置更改时调用。
- onProviderDisabled(String provider) : Called when the provider is disabled by the user. onProviderDisabled(String provider) :当用户禁用提供程序时调用。
- onProviderEnabled(String provider) : Called when the provider is enabled by the user. onProviderEnabled(String provider) :当用户启用了提供程序时调用。
- onStatusChanged(String provider, int status, Bundle extras) : Called when the provider status changes. onStatusChanged(String provider,int status,Bundle extras) :当提供者状态更改时调用。
The android.location
has two means of acquiring location data:
android.location
有两种获取位置数据的方法:
- LocationManager.GPS_PROVIDER: Determines location using satellites. Depending on the conditions, this provider may take a while to return a location fix LocationManager.GPS_PROVIDER :使用卫星确定位置。 根据条件,此提供商可能需要一段时间才能返回位置信息
- LocationManager.NETWORK_PROVIDER: Determines location based on the availability of nearby cell towers and WiFi access points. This is faster than GPS_PROVIDER LocationManager.NETWORK_PROVIDER :根据附近的手机信号塔和WiFi接入点的可用性来确定位置。 这比GPS_PROVIDER快
In this tutorial, we’ll create a Service that implements the LocationListener class to receive periodic location updates via GPS Providers or Network Providers.
在本教程中,我们将创建一个服务,该服务实现LocationListener类,以通过GPS提供商或网络提供商定期接收位置更新。
Android Location API项目结构 (Android Location API Project Structure)
The project consists of a MainActivity.java
class which displays a Get Location and a LocationTrack.java
Service class.
该项目包含一个