TUTORIAL. BUILDING A SIMPLE MAPPING APPLICATION



Similar documents
Getting Started: Creating a Simple App

How to develop your own app

Now that we have the Android SDK, Eclipse and Phones all ready to go we can jump into actual Android development.

Tutorial #1. Android Application Development Advanced Hello World App

Android Programming Basics

Mobile Application Development

Admin. Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources. Recap: TinyOS. Recap: J2ME Framework

Android Services. Android. Victor Matos

Android Concepts and Programming TUTORIAL 1

Introduction to NaviGenie SDK Client API for Android

Android Environment SDK

Developing NFC Applications on the Android Platform. The Definitive Resource

Android Development. Marc Mc Loughlin

Advantages. manage port forwarding, set breakpoints, and view thread and process information directly

Chapter 2 Getting Started

Android Environment SDK

Android Persistency: Files

Introduction to Android SDK Jordi Linares

Android Application Development: Hands- On. Dr. Jogesh K. Muppala

Login with Amazon Getting Started Guide for Android. Version 2.0

How to Create an Android Application using Eclipse on Windows 7

Table of Contents. Adding Build Targets to the SDK 8 The Android Developer Tools (ADT) Plug-in for Eclipse 9

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Creating a 2D Game Engine for Android OS. Introduction

Frameworks & Android. Programmeertechnieken, Tim Cocx

4. The Android System

AdFalcon Android SDK Developer's Guide. AdFalcon Mobile Ad Network Product of Noqoush Mobile Media Group

MMI 2: Mobile Human- Computer Interaction Android

Android Programming. Android App. Høgskolen i Telemark Telemark University College. Cuong Nguyen,

SDK Quick Start Guide

ECWM511 MOBILE APPLICATION DEVELOPMENT Lecture 1: Introduction to Android

Creating a List UI with Android. Michele Schimd

directory to "d:\myproject\android". Hereafter, I shall denote the android installed directory as

OpenCV on Android Platforms

Workshop on Android and Applications Development

Q1. What method you should override to use Android menu system?

Università Degli Studi di Parma. Distributed Systems Group. Android Development. Lecture 2 Android Platform. Marco Picone

Android Tutorial. Larry Walters OOSE Fall 2011

Getting Started With Android

2. Click the download button for your operating system (Windows, Mac, or Linux).

Android Application Development. Yevheniy Dzezhyts

Android Application Development - Exam Sample

Introduction to Android. CSG250 Wireless Networks Fall, 2008

J A D E T U TO R I A L

Android Programming. Høgskolen i Telemark Telemark University College. Cuong Nguyen,

Introduction to Android Programming (CS5248 Fall 2015)

Advertiser Campaign SDK Your How-to Guide

An Introduction to Android Application Development. Serdar Akın, Haluk Tüfekçi

Android Basics. Xin Yang

23442 ECE Introduction to Android Mobile Programming ECE Special Topics: Android Mobile Programming

Jordan Jozwiak November 13, 2011

Android Java Live and In Action

HTTPS hg clone plugin library SSH hg clone plugin library

By sending messages into a queue, we can time these messages to exit the cue and call specific functions.

ID TECH UniMag Android SDK User Manual

Getting Started with Android Programming (5 days) with Android 4.3 Jelly Bean

Specialized Android APP Development Program with Java (SAADPJ) Duration 2 months

ELET4133: Embedded Systems. Topic 15 Sensors

Presenting Android Development in the CS Curriculum

GearVRf Developer Guide

How To Develop Android On Your Computer Or Tablet Or Phone

How To Develop An Android App On An Android Device

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Android Introduction. Hello Mihail L. Sichitiu 1

Programming with Android: SDK install and initial setup. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Developing Android Apps: Part 1

06 Team Project: Android Development Crash Course; Project Introduction

Mobile Application Development Connecting with PHP REST Servers from Android

Android Bootcamp. Elaborado (com adaptações) a partir dos tutoriais:

Developing In Eclipse, with ADT

Building Your First App

Lab 1 (Reading Sensors & The Android API) Week 3

Operational Decision Manager Worklight Integration

Homework 9 Android App for Weather Forecast

An Android-based Instant Message Application

IOIO for Android Beginners Guide Introduction

Android Apps Development Boot Camp. Ming Chow Lecturer, Tufts University DAC 2011 Monday, June 6, 2011

What is Android? originally purchased from Android, Inc. in 2005

Basics of Android Development 1

Android App Development. Rameel Sethi

Android Development Tutorial. Nikhil Yadav CSE40816/ Pervasive Health Fall 2011

App Development for Smart Devices. Lec #4: Services and Broadcast Receivers Try It Out

INTRODUCTION TO ANDROID CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 11 02/15/2011

Android Basic XML Layouts

Backend as a Service

IBM Tealeaf CX Mobile Android Logging Framework Version 9 Release 0.1 December 4, IBM Tealeaf CX Mobile Android Logging Framework Guide

Transcription:

Cleveland State University CIS493. Mobile Application Development Using Android TUTORIAL. BUILDING A SIMPLE MAPPING APPLICATION The goal of this tutorial is to create a simple mapping application that we will call Adonde? We assume you have installed the most recent Android SDK [1], you have obtained a Google Maps API key [3], and your Eclipse IDE includes the ADT plugin [2]. The user of this application will type in a valid address and a map of the given place will be drawn on the phone s screen. Figure 1-(Left) depicts on a road map the location of Cleveland State University. The expected input is an address such as 123 Main Ave. Cleveland OH, or a location such as Rock and Roll Hall of Fame. Observe the map includes a view control to zoom in and out. You can also touch the screen and drag the map on any direction. As you already know, when a new Android application is created the Eclipse ADT provides the basic skeleton of the solution. The code is stored in a package inside the /src folder; resources such as pictures, files, etc. are stored in the /res subdirectory (see Figure 1-Right). In our example the class Adonde extends Android s native MapActivity interface which provides means of displaying and navigating a map held in a MapView container. The GUI definition holding the MapView widget- is stored as an XML document in the /res/layout/main.xml file. The AndroidManifest.xml file contains a list of all the activities, and special permissions requested by the application. Now we are ready to work on the program. Figure 1: (Left) Screen-shot of the Android Adonde application showing a map and coordinates of Cleveland State University. (Right) Structure of the app shown from Eclipse s Package Explorer. V. Matos Adonde Tutorial - 1

CREATING THE APPLICATION STEP 1. Open the AndroidManifest.xml file and add the following statement as a child of the <application> element: <uses-library android:name="com.google.android.maps" /> We need Internet access to retrieve the Google Maps tiles, so the application must request the INTERNET permissions. In the AndroidManifest file, add the following statement as a child of the <manifest> element: <uses-permission android:name="android.permission.internet" /> STEP 2. Now open the main layout file for your project. Define a nested linear layout to hold the GUI component as indicated below. <?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" > <TextView android:id="@+id/mycaption" android:layout_width="wrap_content" android:text="address/coordinates" /> <LinearLayout android:orientation="horizontal" > <EditText android:id="@+id/myaddress" android:layout_width="260px" android:hint="enter location (address)" android:textsize="18sp" /> <Button android:id="@+id/mybtnsearch" android:layout_width="59px" android:text="go" /> </LinearLayout> <com.google.android.maps.mapview android:id="@+id/mymap" android:apikey= "Your personal Google Maps key goes here" android:layout_height="350px" android:clickable="true" /> </LinearLayout> A personal Google Map Key is required for each developer. V. Matos Adonde Tutorial - 2

The android:clickable= true clause consents to user-interaction with the map. The android:apikey attribute holds the public Google Maps API Key obtained after you register with the Google Maps service[5]. STEP 3. Now open the Adonde.java file. Instead of a general purpose Activity class we want to override the specialized MapActivity interface. We also need to add our rendition of the mandatory isroutedisplayed() method. Change the class declaration to: public class HelloMapView extends MapActivity { @Override protected boolean isroutedisplayed() { return false; Continue working with the Adonde class, now add a definition for our class variables private TextView lblcaption; private EditText txtaddress; private Button btnsearch; private MapView mymap; private double lat, lon; We proceed overriding the oncreate method. Here the GUI components introduced in the XML layout will be wired-up to the Java objects defined above. A call to findviewbyid associates the internal object identifier given to a GUI object to its corresponding class variable. For instance txtaddress is the object handler for the GUI EditText control defined as myaddress. @Override protected void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.main); txtaddress = (EditText) findviewbyid(r.id.myaddress); lblcaption = (TextView) findviewbyid(r.id.mycaption); btnsearch = (Button) findviewbyid(r.id.mybtnsearch); mymap = (MapView) findviewbyid(r.id.mymap); mymap.setbuiltinzoomcontrols(true); The method setcontentview loads the GUI file (main.xml) on the screen. The last statement adds zooming capabilities to the MapView. Now we introduce the command button and its listener. Type in the following code: btnsearch = (Button) findviewbyid(r.id.mybtnsearch); btnsearch.setonclicklistener(new OnClickListener() { public void onclick(view v) { geoc = new Geocoder(getApplicationContext()); V. Matos Adonde Tutorial - 3

String inputaddress = txtaddress.gettext().tostring(); try {//make an Address list with top 3 matches List<Address> lstfoundaddresses = geoc.getfromlocationname( inputaddress, 3); if (lstfoundaddresses.size() == 0) showmsg("nothing found - Invalid address"); else { navigatetolocation(lstfoundaddresses.get(0), mymap ); catch (Exception e) { showmsg(e.getmessage()); // onclick ); // btnsearch GeoCoder is an Android class used to associate a physical address to its coordinate values (stored in a GeoPoint as latitude and longitude in microdegrees). When the command button is pressed the string in the txtaddress box is passed as a GeoCoder object to the method getfromlocationname. This method makes an Internet call to the Google Maps Service server and returns a list of the best n matches. In our example we ask for 3 options, however we just look at the first address and ignore the rest of the list. This process is called reverse geo-coding because we go from a textual address to a numeric coordinate value. The method navigatetolocation draws the map centered on the given latitude and longitude pair. Continue the tutorial by entering the code: public void navigatetolocation( Address adr, MapView map) { try { //covert coordinate microdegrees to integer values double latitude = adr.getlatitude() * 1000000; double longitude = adr.getlongitude() * 1000000; lblcaption.settext("lat:" + latitude + " Lon: " + longitude); GeoPoint geopt = new GeoPoint((int) latitude, (int) longitude); MapController mapctrl = map.getcontroller(); mapctrl.animateto(geopt); // move map to the GeoPt mapctrl.setzoom(17); // zoom at chosen level mapctrl.setcenter(geopt); // center the map on GeoPt map.setsatellite(false); // display road-map view map.settraffic(false); // do not show traffic info catch (Exception e) { showmsg( e.getmessage() ); // navigatetolocation In this fragment the coordinates found earlier are used to center the corresponding map. In our example the location is displayed using a zooming factor of 17 (max is 21), and intentionally disregard satellite and traffic view. The map is clickable therefore it could be dragged and resized. We need just to add our showmsg method; it is used to display messages that fade from the screen after a few seconds. V. Matos Adonde Tutorial - 4

private void showmsg(string themsg){ Toast.makeText(getApplicationContext(),theMsg, Toast.LENGTH_SHORT).show(); //showmsg at this point our application is ready to run! 6. REFERENCES [1] Android Developer s Guide available at: http://developer.android.com/ [2] Eclipse IDE Plugin for Android. Available at www.eclipse.org. Accessed Feb 2010. [3] Obtaining a Google Maps API Key. Available at http://code.google.com/android/addons/google-apis/mapkey.html. Accessed Feb 2010. [1] Matos, Victor. CIS493 Special Topics in Computer Science. Available at: http://grail.cba.csuohio.edu/~matos/notes/cis-493/android-syllabus.pdf. Accessed February 2010. [2] Matos, Victor and Ben Blake. I am OK - A Conceptual Model for a Global Emergency System and Its Societal Impact. International Journal of Technology, Knowledge and Society. Vol. 2 No. 5, pp.7-18, January 2007. V. Matos Adonde Tutorial - 5