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

(gtxtreme123) #1

Flickr Geosearch


Listing 33.11  Listening for connection events (LocatrFragment.java)


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);


mClient = new GoogleApiClient.Builder(getActivity())
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
getActivity().invalidateOptionsMenu();
}


@Override
public void onConnectionSuspended(int i) {


}
})
.build();
}


If you are curious, you can hook up an OnConnectionFailedListener and see what it reports. But it is
not necessary.


With that, your Google Play Services hookup is ready.


Flickr Geosearch


The next step is to add the ability to search for geographic locations on Flickr. To do this, you perform
a regular search, but you also provide a latitude and longitude.


In Android, the location APIs pass around these location fixes in Location objects. So write a new
buildUrl(...) override that takes in a Location object and builds an appropriate search query.


Listing 33.12  New buildUrl(Location) (FlickrFetchr.java)


private String buildUrl(String method, String query) {
...
}


private String buildUrl(Location location) {
return ENDPOINT.buildUpon()
.appendQueryParameter("method", SEARCH_METHOD)
.appendQueryParameter("lat", "" + location.getLatitude())
.appendQueryParameter("lon", "" + location.getLongitude())
.build().toString();
}


And now write a matching searchPhotos(Location) method.

Free download pdf