DZone
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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Android Location with Google Maps - Part 1

Android Location with Google Maps - Part 1

Tony Siciliani user avatar by
Tony Siciliani
·
Jun. 21, 12 · Interview
Like (0)
Save
Tweet
Share
6.58K 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

  • Best Practices for Setting up Monitoring Operations for Your AI Team
  • Leverage Lambdas for Cleaner Code
  • Isolating Noisy Neighbors in Distributed Systems: The Power of Shuffle-Sharding
  • Scaling Your Testing Efforts With Cloud-Based Testing Tools

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: