DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Android Location with Google Maps - Part 1

Android Location with Google Maps - Part 1

Tony Siciliani user avatar by
Tony Siciliani
·
Jun. 21, 12 · Java Zone · Interview
Like (0)
Save
Tweet
6.36K Views

Join the DZone community and get the full member experience.

Join For Free

1.  requirements: what do we want?


let's say we want to create an application that displays the phone's location on a map, plus other cool stuff on top of that, like the possibility to send the location via sms or email to contacts simply by tapping the screen (for example). in order to do that, we need several things:

  1. the ability to access the phone's location services
  2. a visual display of the phone's location on a map
  3. the capacity to react to changes in location and update the map accordingly
  4. the possibility to overlay items on top of the map, like icons, dialogues or other clickable items

2.   tools and apis: what do we have?

android provides access to the phone's location services with the locationmanager in the android.location package . the api provides some level of useful information such as:

 // get a reference to the system location manager via the app context
 locationmanager locmanager = 
      (locationmanager) context.getsystemservice(context.location_service);

// 1. list of providers (network, gps, etc..)
 list providers =  locmanager.getallproviders();

// 2. best provider using some criteria. here, the default
string bestprovider = locmanager.getbestprovider(new criteria(), false);

// 3. last known location
location lastknown = locmanager.getlastknownlocation(bestprovider);
string display = new stringbuilder()
      .append("latitude: ").append(lastknown.getlatitude())
      .append("\nlongitude: ").append(lastknown.getlongitude())
      .append("\naltitude: ") .append(lastknown.getaltitude())
      .append("\naccuracy: ").append(lastknown.getaccuracy())
      .append("\nbearing: ").append(lastknown.getbearing())
      .append("\nspeed: ").append(lastknown.getspeed())
      .tostring();
// show display on screen
//...

and that's nice...if you like numbers. but to actually show the location on a map, we need an external  library like google maps . and to use that, we need to do a couple of things:

  • download it via the sdk manager
  • obtain a maps api key

once we do the above (and that's relatively painless), we can start creating a simple application that displays our location on a map. so that takes care of requirements 1 and 2 above.

the third requirement, i.e. the capacity to update the phone's location is provided by implementing one or more of the android.location.locationlistener 's callback methods:

public void onlocationchanged(location location) {}
public void onstatuschanged(string provider, int status, bundle extras) {}
public void onproviderenabled(string provider) {}
public void onproviderdisabled(string provider) {}

finally, our fourth requirement will be met by extending google maps' overlay , overlayitem or itemizedoverlay . the latter provides us with a list of items we might want to overlay on top of our map. in particular, we will be interested in the following method :

protected boolean ontap(int index){}

...since we would want the user to get some added functionality by tapping his/her location on the map. that's it. now that we have all we need to start designing and coding our application.

3.  application design

at first glance, we'll need three classes with each their own responsibilities:

  1. the ui, i.e. an android activity class extending google maps mapactivity : locatoractivity
  2. a processor class that will take care of all the location-based logic, and in particular react to location updates: locator
  3. an overlay class which will focus on the items to display on top of the map: locatoroverlay

we might need more than the above as we dive more deeply into our implementation, but remember, we are agile, so we can always go back & modify our original design. this is our (simplified) uml class diagram:

we'll start tackling the implementation in part 2 , starting with the lowest-level class, i.e. locatoroverlay .

from tony's blog .

Google Maps Google (verb) Android (robot)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Engineer’s Guide to Creating a Technical Debt Proposal
  • Migrating From Heroku To Render
  • Testing Under the Hood Or Behind the Wheel
  • Fintech and AI: Ways Artificial Intelligence Is Used in Finance

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo