2016年10月12日 星期三

Google Play Services -> Basic location



Set Up Google Play Services


To access the fused location provider, your app's development project must include Google Play services. Download and install the Google Play services component via the SDK Manager and add the library to your project. For details, see the guide to Setting Up Google Play Services.

Specify App Permissions


Apps that use location services must request location permissions. Android offers two location permissions: ACCESS_COARSE_LOCATION andACCESS_FINE_LOCATION. The permission you choose determines the accuracy of the location returned by the API. If you specifyACCESS_COARSE_LOCATION, the API returns a location with an accuracy approximately equivalent to a city block.
This lesson requires only coarse location. Request this permission with the uses-permission element in your app manifest, as the following code snippet shows:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.location.sample.basiclocationsample" >

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
</manifest>

Connect to Google Play Services


To connect to the API, you need to create an instance of the Google Play services API client. For details about using the client, see the guide toAccessing Google APIs.
In your activity's onCreate() method, create an instance of Google API Client, using the GoogleApiClient.Builder class to add the LocationServicesAPI, as the following code snippet shows.
// Create an instance of GoogleAPIClient.
if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
}

沒有留言:

張貼留言