Sunday, March 4, 2012

Android: Using Google maps app to show places in your application

When we are developing android application with maps functionality normally developers tries to use Google maps API for this. But if your requirement is simple it may be more easy and effective to use existing maps application by a intent. In this application I'm going to show how to show the nearest hospital in your application.

Note- To make this work your app and emulator must be created using Google APIs not android APIs. (Both are the same but if you are not using Google maps API map application functionality is not available to you).

Here is the code,

LocationManager  locMgr  = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location recentLoc = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double lat = recentLoc.getLatitude();
double lon = recentLoc.getLongitude();
String geoURI = String.format("geo:%f,%f?q=hospital", lat, lon);
Uri geo = Uri.parse(geoURI);
Intent geoMap = new Intent(Intent.ACTION_VIEW, geo);
startActivity(geoMap);


In the geoURI string you can change the hospital in to any other place. If you just wan't to center the map to your location do not include the question mark and latter part in geoURI .

This is how my application shows the nearest hospital to my location.

If you test this in Android emulator you have to send your location to the emulator using DDMS in eclipse because. Otherwise it will throw a null pointer exception. You can find articles on how to do this in internet. 

4 comments:

  1. Thanks . Nice work Your intent example useful for me .

    ReplyDelete
  2. Report Bugs Topic tells about the bug reports of this blogs.... Thanks for sharing...

    Android Training in Chennai

    ReplyDelete
  3. Am using Android studio with enable apis and after integrate this code and run then showing error:please enable api in developer console link but am already enabled

    ReplyDelete
  4. can u send the source code of project?

    ReplyDelete