First Android app: Earthquake

Today I will remake the example application in the book by Reto Meier Professional Android 2 Application Development published by Wrox. (ISBN: 978-0-470-56552-0)

Indeed since the USGS revised its information flows, the application crashes at start becoming useless. And as I liked having information on earthquakes, i’ll do the update. Moreover, this is an opportunity to work with the OpenStreetMap API for Android.

Install the work environment

My first task was to download an appropriate development environment: Android Studio . http://developer.android.com/sdk/installing/studio.html

I moved and I spear. Whoops! An error message …

Error launching Android Studio

Ok, obviously it isn’t know the PATH until my JDK.
I reinstall the java Jdk in its latest version. http://www.oracle.com/technetwork/java/javase/downloads/index.html

But it still crashes with the same error message. Fortunately Stackoverflow gives me the answer. In fact we must add two paths in the environment variables:

      • JAVA_HOME: the path to the root of the JDK
      • PATH: the path to the bin directory of the JDK
JAVA_HOME C:/Program Files/Java/jdk1.7.0_40/jre
PATH C:/Program Files/Java/jdk1.7.0_40/jre/bin 

We restart the Android Studio.

Android Studio

A nice loader screen: everytghing is ok.

I create a new project: File> New Project.

I filled out the necessary information for my project.

Nouveau projet 1 / 4 Nouveau projet 2 / 4 Nouveau projet 3 / 4 Nouveau projet 4 / 4

And it creates an initial files tree, compile newly created files
Building gradle
and shows us a Hello World in a pretty Nexus 4.
Environnement de développement

Working with Open Street Map

I will use osmdroid ( http://code.google.com/p/osmdroid/ ) that displays a map using OpenStreetMap tiles ( http:/ / www.openstreetmap.org ). It can be used free and without application-specific key, unlike Google Map (https://developers.google.com/maps/documentation/android/ ).

So I downloaded the Jar of osmdroid and that of SLF4J Android which osmdroid depends on.

and I copy these two jar with windows explorer in a libs folder created in Earthquake folder of my project, next to src and build .

copie-libs

Then in Android Studio, I’ll include these two libraries.
In the project tree, I select the two items and right-click menu shows me the following:
add_as_library

I chose Add as library .

Now I can edit the file src/main/res/layout/activity_main.xml.
I delete the default Hello World and I put a Mapview instead:

<org.osmdroid.views.MapView
   android: id = "@id/mapview"
   android: layout_width="match_parent"
   android: layout_height="match_parent"
/>

You should also add this to the src/main/AndroidManifest.xml file for the application to load the Mapview and the Mapview to work.

<uses-library android:name="org.osmdroid.views.MapView" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Then i edit the file MainActivity.java with this code:

ch.moutons.earthquake package;
import android.os.Bundle;
import android.app.Activity;
import org.osmdroid.views.MapView;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.overlay.OverlayItem;
import org.osmdroid.views.overlay.ItemizedOverlay;
import org.osmdroid.views.overlay.ItemizedIconOverlay;
import org.osmdroid.ResourceProxy;
import org.osmdroid.DefaultResourceProxyImpl;
public class extends Activity {MainActivity
MapView map;
@Override
protected void onCreate (Bundle savedInstanceState) {
   super.onCreate (savedInstanceState);
   setContentView (R.layout.activity_main);
   map = (MapView) findViewById (R.id.mapview);
   map.setTileSource (TileSourceFactory.DEFAULT_TILE_SOURCE);
   GeoPoint startPoint = new GeoPoint (52370816, 9735936);
   MapController mapController map.getController = ();
   mapController.setCenter (startPoint);
   map.setBuiltInZoomControls (true);
   mapController.setZoom (2);
   map.setUseSafeCanvas (true);
   map.setUseDataConnection (true);
}

I make a test by pressing the Run button run to start the compilation and test it in the debugger.
Premier essai
We see that the application is loaded and the zoom works but osmdroid does not display the images of the map. This may be due to the debugger. Actually exporting the application on my phone displays the map perfectly.

2013-10-06 12.13.16

Ok, it rolls

Geographic Coordinates

I now write a class to search the geographical coordinates of the epicenters of earthquakes.

The USGS (United States Geological Survey – http://www.usgs.gov/ ) publishes on its website information on the latest earthquakes recorded: http://earthquake.usgs .gov/earthquakes/feed/v1.0 /.

I utiliseGeojson ( http://geojson.org/ ) to decode the JSON feed from the USGS once coordinates collected in a Hashtable, you can display them on the map.

Click on a point to display a Toast http://developer.android.com/guide/topics/ui/notifiers/toasts.html

AND finally, I get this gorgeous map with earthquakes.

2013-10-08 09.11.20

Download the apk file: earthquake.apk (808Ko)
Download the source code of the application: EarthquakeProjectzip (6MB)