How To Build Android Apps Using Google Maps API

You wanna build an android app using Android Google Maps API? If you answered yes, stick around. I have something good for you. At the end of this post, you can checkout my repository for the source code for this simple android app unnecessarily named “KENYAPP”. Well, it is an app about Kenya. You can change the name to what you want. Also change the coordinates to your own country.

Android Google Maps API Demo App End Product

As you can see, on the left of the top part is a list of Provinces and on the right hand side is a map with a marker on it. Below the map are several things;

A TextView showing the title (question or whatever you want)

Three column row with different information displayed and styled.

The Android App Functionality:

There is nothing fancy here. The app simply shows you where a given province is located in the map of Kenya, zooms in close enough for you to see and populates the details at the bottom. I am planning to add more stuff later when I get time.

This app might be simple but it uses some highly recommended designs like using fragments in Android. It also uses Google Maps API v2 (Android) and some concepts like communication between activities and fragments and list adapter for managing the list items.

Enough with the jibby jabber. Let us see the code here:

Like a good coder, it is good to start with the layouts. At least that is what I do. So, here is my main activity layout.

As you can see, this file contains several things. FrameLayout, TableLayout among others. I am not going to turn this post into a tutorial on what FrameLayouts, TabletLayouts etc are. If you need more info on those components, Android documentation is a great place to start. I am also not saying that my way of laying out my app is the best; You can always improve it.

You can see that there is a map fragment inside the first row of the table near the top of the file. That is important because it will contain our map. Just above it, I have a container that will hold my list which will be a fragment.

NOTE: I would recommend not copying this code from here because it might have errors due to the highlighter misbehaving. Instead, clone it or copy it from my repository:here

Secondly, we need another layout file for the list display (inflated by the fragment). Here is that file:provinces_frag.xml

This file contains the ListView component and a TextView for situations where data is not available.

Lastly, we have another xml file for a single row (list item). You can use the one that comes with the SDK but if you want to customize the look of the items, you have to create your own layout. Simple to do:

simple_row_item.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

< ?xml version="1.0"encoding="utf-8"?>

<textview xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/text1"

android:textSize="15sp"

android:textStyle="normal"

android:paddingTop="15dp"

android:paddingLeft="0dp"

android:paddingBottom="15dp"

android:gravity="center_horizontal"

android:layout_gravity="center_vertical|fill_vertical"

android:textColor="@drawable/list_item_text_selector"

android:layout_width="fill_parent"

android:layout_height="wrap_content"></textview>

If you choose to use this code, copy it as it. Do not place the TextView inside another layout component. It has to be on its own or you will get errors. Good enough.

Now, I have two more files: The main activity and the fragment that holds the list. Again, you can easily copy all the files from my repository later, including the drawables.

Summary of Android Google Maps API v2

There are a lot of things I could do to improve this piece of code. There might be design flaws and stuff like that. My goal was to show you how to use several things: Activities, Fragments, Third party APIs, custom list view and such.

In the future, I will be adding more tutorials on different Android concepts like running networking operations in an AsyncTask instead of on main thread. Stay tuned.

Please share this post with your friends if you found it helpful and you can ask any questions you might have. Thank you for stopping by.

Written By Elisha Chirchir

Elisha Chirchir is a software developer. He is also the founder of Simple Developer and co-founder of Instinctive Software Solutions. On any given day, he works on both Android and Web Development. During his 'free time', he offers training to those interested in learning how to code in php, java, python, javaScript etc. You can easily find him on StackOverflow Android chatroom or on Twitter @Eenvincible