sábado, 16 de junho de 2012

Creating an Android App with Google Maps Integration (Part 2)

Regarding the part 1 of this tutorial: from this point i'm going to start using Android 2.3.3 on the emulator because the Android 4.0.3 gave me some problems.

Adding a mapView to your Android App

  • First we need to tell to our app where is library for the google maps. Go to the AndroidManifest.xml file and under <application> add this:
<uses-library android:name="com.google.android.maps" />

  • Now, because we want to get the maps from google we need to the app to access the internet. To do that we also need to add this line under the <manifest>:
<uses-permission android:name="android.permission.INTERNET" />

  • Go to the res/layout/main.xml file and replace all the xml with this one:
  • Now we need to get the Maps API key to insert on "Insert Maps API key here". To do that open the command line, go to the directory of the debug keystore (if you don't now yours go to eclipse preferences) and run this comand:
  • If it asks about a password just press enter.
  • You should see now a MD5 hash. Copy it and paste it in here to register your key: https://developers.google.com/android/maps-api-signup
  • Now you have your key to insert on the App. Do it.
  • Go to the main activity (mine is FoursquareMapActivity) and extend the class to MapActivity.
  • From here we just need to override some methods and add two lines of code to add zoom in and out buttons:
  • If you have errors in your code press "Ctrl + left shift + O" to add the imports that may be missing.
  • If you have your emulator running you can run your app and see the google maps working :).
  • To add a little more space we can take out the main title by going to the manifest and add this line 
    android:theme="@android:style/Theme.NoTitleBar"
    inside <activity>

quinta-feira, 14 de junho de 2012

Creating an Android App with Google Maps Integration (Part 1)

Creating apps for Android

For developing in Android i'm using Eclipse IDE, and there is almost any difference in using it on Linux as in Windows, so, this tutorial should work for both OS's.

First thing to do is download the Android SDK and the ADT Plugin for Eclipse. However, i'll not cover that (for now). It should be easy if you follow this instructions:

(...after that is done...)

Go to the Window menu and open the Android SDK Manager.

Select the folder Tools and select one Android version. I'm using the Android 4.0.3 (API 15) (you can select any of them, but if you want to develop an app that works in every single android phone you should choose Android 1.5). After you select everything you want, click the Install packages button, select accept all and proceed.


Now we can create our project.

  • Go to the menu File, then New and click Project...
  • Select the folder Android, then Android Project and click the Next button.
  • In the project name write the name of your project. Mine is "FoursquareMap" (because i'm going to use this to write about the foursquare API in the future).
  • You can ignore the rest and click Next.
  • Now, here is where you should select the android version for witch you are developing. However we are not going to select Android x.x.x because we want to make an google maps app. So to do this, choose Google API's, but be sure to select the google API with the API Level that you want (for example the API Level for android 4.0.3 is API Level 15).
  • In the next window, choose the name of your application, your package name and click Finish.
And just like that, we have an app for android. To test first we need to create an emulator to run it.

  • Go to the menu Window, and open AVD Manager.
  • Click New, give it a name, for example, "my_avd".
  • And in target choose the Google API with the API Level that you want.
  • Click Create AVD.
  • Select the just created AVD and click on the button Start and then the button launch.
  • Wait for the AVD to complete his initialization (when you reach the lock screen like if you were on a real android device).

  • Go back to eclipse, run your project and go back to the AVD. In a few seconds it should launch your app.