Saturday, October 22, 2016

Google Maps V2 Android Tutorial 0 - Setup

The Google Maps V2 Android API is very useful for building Android apps that need a map or some sort of navigating platform. These next series of tutorials will teach you how to build your own app while using Google Maps.

In this first tutorial, we are simply going to go through setup:

1. The very first thing we need to do is to download Android Studio.

2. Then we need to add google play services to android studio.

3. Start Android Studio

4. If you see the welcome dialog, choose Start a new Android Studio Project, available under quickstart. Otherwise, click File>New>New Project

5. Enter your app name, company domain, and project location. Click Next.

6. Enter activity name, layout name, and title. Click Finish.

7. After the gradle is done building, go to google_maps_api.xml

8. Follow the instructions in that file.

You have now completed setup, let's take a look at the code that has been automatically generated by android studio.

The XML File

This XML file can be found in your projects tab under res/layout/YOUR_ACTIVITY_NAME.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context=".MapsActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment" />
This file defines how your app will appear on the screen when your app is compiled and built to an android device. Using Android Studio, you can fiddle around with the widgets and add buttons, textboxes, popups, controls, etc. We will go further into detail about the activity.xml file in future tutorials.

The Maps Activity Java File

This file can be located in your projects tab under java/com.xxx.xxx/YOUR_ACTIVITY_NAME.java. This file contains the code that will run when you start your Google Maps Application. By default, your activity.java file should look something like this:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney, Australia, and move the camera.
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

Android Manifest File

The last file we will be looking at is the android manifest file which can be found in the project tab under manifests/AndroidManifest.xml. This file contains all the permissions, details, libraries, and specifications about your application. The android manifest is vital to your program as it is needed for any application before it can open and run successfully.

Throughout the construction of your Google Maps Android Application, you will be mainly modifying these three files.

As of right now, you are probably going to need a physical android device to run your Google Maps Application since android virtual device does not have location services capability. Make sure developer tools is enabled on your android device. Then, plug in your android device and click the green play button at the top of android studio. This compiles your code and installs the apk (Android Package Kit) into your android device. Run the app to make sure it's working. By default, it should be showing a simple map with which you can zoom and move round.

In the next tutorial, I will be showing you how to play with markers as well as use the location services of your device and integrate into your application. Stay tuned!