Generating the Events Locations through Geo-Location

One of the requirements of the assignment was to use geo-location to give location details of where the events were going to take place. The longitudinal and latitudinal details are being parsed from the database, and are included in the location details for each event. Since Google Maps API is one of the best geo-location services and since it is easily integrated to any Android App, this particular API will be used in the project to get the location of each event.

In order to use google maps one first needs to obtain a map key through the Google API console. Assuming that a Windows PC is being used, the is the way it is done using MD5 key encryption from the jdk installation.

Through the command prompt, one needs to run the following script:

c:\<path-to-jdk-dir>\bin\keytool.exe -list -alias androiddebugkey -keystore "C:\users\<user-name>\.android\debug.keystore" -storepass android -keypass android

This would output the MD5 key similar to the one below:

keytool.exe -list -alias androiddebugkey -keystore "C:\users\mlaferla\.android\debug.keystore" -storepass android -keypass android

Using the Google API Console, the generated key needs to be uploaded and regstered to use the Google Maps API for Android devices. This associated the map we will be using to the key by giving it the MD5 fingerprint. The reason for doing so is to associate the Android application with the Maps API. Otherwise, the map cannot be rendered on the app.

The hashed code given by the Google API Console needs to then be copied as is into the manisfest to be able to call it correctly when using the app.

Below is the xml content to generate the map screen:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#F38121" >
<fragment
 android:id="@+id/mapView"
 android:name="com.google.android.gms.maps.SupportMapFragment"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_margin="15dp"
 android:padding="15dp" />
</RelativeLayout>

And here below, the code to build the map screen:

package com.chaplaincy.jc.tibiapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
/**
 * Created by mlaferla on 17/02/2015.
 */
public class MapFragment extends Fragment {
private SupportMapFragment fragment;
 private GoogleMap map;
private int mapType = GoogleMap.MAP_TYPE_NORMAL;
@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 return inflater.inflate(R.layout.fragment_googlemap, container, false);
 }
@Override
 public void onActivityCreated(Bundle savedInstanceState) {
 super.onActivityCreated(savedInstanceState);
 FragmentManager fm = getChildFragmentManager();
 fragment = (SupportMapFragment) fm.findFragmentById(R.id.mapView);
 if (fragment == null) {
 fragment = SupportMapFragment.newInstance();
 fm.beginTransaction().replace(R.id.mapView, fragment).commit();
 }
 }
@Override
 public void onResume() {
 super.onResume();
 if (map == null) {
 map = fragment.getMap();
 map.addMarker(new MarkerOptions().position(new LatLng(37.7750, -122.4183)));
 }
 }
@Override
 public void onSaveInstanceState(Bundle outState) {
 super.onSaveInstanceState(outState);
// save the map type so when we change orientation, the mape type can be restored
 LatLng cameraLatLng = map.getCameraPosition().target;
 float cameraZoom = map.getCameraPosition().zoom;
 outState.putInt("map_type", mapType);
 outState.putDouble("lat", cameraLatLng.latitude);
 outState.putDouble("lng", cameraLatLng.longitude);
 outState.putFloat("zoom", cameraZoom);
 }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.