Have you ever been out walking and wondered if DRI contains any objects related to your current location?
No, neither have I, but having taken a short online course in Android development I thought I’d put together
a simple app to do this.
First we need a basic Android application that will display a map. There are plenty of guides
available on how to do this, for example here.
Next is to find out where the user is and to receive updates on their location
as they move. How to do this is covered well in the Android developer guide.
A very brief summary is:
Give the application permission to access location services in the app manifest
Create an API client and a LocationRequest object
Once the client is connected (i.e., in the onConnected() callback) request location updates
The current location will be updated through the onLocationChanged() callback
Each time we receive a location update we process it in the handleNewLocation() method. In this method
the user’s current latitude and longitude are retrieved, a marker is added to the map, and this location
is then passed to the code that handles finding the nearby objects from the DRI repository.
In a previous post I talked about mapping in the DRI repository. Adding the mapping interfaces involved configuring Solr
to support spatial searches. We can use this functionality here to retrieve the objects close to the user’s location.
The url to perform a spatial query looks like this:
The response is in JSON format and contains the Solr documents of all the objects matching the query (i.e., all the objects
within a certain spatial distance of the given co-ordinates). Rather than display every object the app will only add
markers at each place, with the number of objects at that place added as a label. Querying the repository and processing
the results is all performed in an AsyncTask separate to the MainActivity. This is done as the network request
and subsequent processing could take some time and the app should not stop responding during this.
To retrieve the response a connection is made to the search URL followed by a GET request
contentAsString contains the JSON response and this is returned for parsing.
The org.json API is used to parse the JSON. As stated above, as the app will
only display counts of the objects at locations we can parse the facet output contained in the response
rather than each object.
For each entry added to the placeList a marker is added to the map.
The marker shows the number of objects.
Clicking on the marker displays the placename.
Clicking on the title opens the device’s browser and displays the objects in the repository that contain that placename in their metadata.
The screenshots above show the working application running on an emulator of a Nexus 5.