Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 33  Locations and Play Services


Listing 33.13  New searchPhotos(Location) (FlickrFetchr.java)


public List searchPhotos(String query) {


}


public List searchPhotos(Location location) {
String url = buildUrl(location);
return downloadGalleryItems(url);
}


Getting a Location Fix


Now that you have everything set up, you are ready to get a location fix. Your window to the Fused
Location Provider API is a class named, appropriately enough, FusedLocationProviderApi.
There is one instance of this class. It is a singleton object that lives on LocationServices called
FusedLocationApi.


To get a location fix from this API, you need to build a location request. Fused location requests
are represented by LocationRequest objects. Create one and configure it in a new method called
findImage(). (There are two LocationRequest classes. Use the one with the complete name of
com.google.android.gms.location.LocationRequest.)


Listing 33.14  Building a location request (LocatrFragment.java)


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
...
}


private void findImage() {
LocationRequest request = LocationRequest.create();
request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
request.setNumUpdates(1);
request.setInterval(0);
}
}


LocationRequest objects configure a variety of parameters for your request:



  • interval – how frequently the location should be updated

  • number of updates – how many times the location should be updated

  • priority – how Android should prioritize battery life against accuracy to satisfy your request

  • expiration – whether the request should expire and, if so, when

  • smallest displacement – the smallest amount the device must move (in meters) to trigger a
    location update

Free download pdf