Sunday, December 11, 2011

Location based service using Google maps api

After lot of procrastination i thought lets do it and publish first post on Android.

Environment :
Android API 8(Froyo 2.2)
Maps.jar (can be downloded from Android SDK manager)
Preferably eclipse with ADT plugin

Steps :
1. Create New>Android Project>Project Name>Target - Google API's>Package Name>Min SDK(8)
2. Open AndroidManifest.xml
Grant following permissions:

        <uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Third one is optional(not required on real device)
Add following library
<uses-library android:name="com.google.android.maps" android:required="true"/>
3. Open Main.xml

Add following layout in Relative layout

<com.google.android.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="XXXXXX"/>
You can get map key by following this link
http://code.google.com/android/add-ons/google-apis/mapkey.html

3. Open your Activity file
Extend MapActivity instead of Activity class
In onCreate method get mapView
     mapView = (MapView) findViewById(R.id.mapView);
get LocationManager using
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MINIMUM_TIME_BETWEEN_UPDATES,
MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new cLocationListener());
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
GeoPoint geoPoint = new GeoPoint((int)(l.getLatitude()* 1E6), (int)(l.getLongitude()* 1E6));
Get MapController
MapContoller mc = mapView.getController();
mc.animateTo(geoPoint);

Now here is our listener class, after all someone has to listen for change

private class cLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude());
Toast.makeText(MapTestActivity.this, message, Toast.LENGTH_LONG)
.show();
Toast.makeText(MapTestActivity.this, message, Toast.LENGTH_LONG).show();
}

public void onStatusChanged(String s, int i, Bundle b) {
Toast.makeText(MapTestActivity.this, "Provider status changed",
Toast.LENGTH_LONG).show();
}
//GPS related toast
public void onProviderDisabled(String s) {
Toast.makeText(MapTestActivity.this,
"Provider disabled by the user. GPS turned off",
Toast.LENGTH_LONG).show();
}

public void onProviderEnabled(String s) {
Toast.makeText(MapTestActivity.this,
"Provider enabled by the user. GPS turned on",
Toast.LENGTH_LONG).show();
}
}


You can use NETWORK_PROVIDER to get location, just need a real device but GPS_PROVIDER can be used in AVD also.

Switch to DDMS perspective>Emulator Control>Scroll down to Location Control>click on send



Troubleshooting :
I got two issues
1. ClassNotFound because target SDK was 2.2, it should be Google API's
2. Because AVD target was again 2.2, changed to Google API's

Final result on Olive Pad(Froyo)..ya that's lat long of Gurgaon.





Interesting Fact : on a final thought do you know that Android naming convention is based on desserts, and this is big one  ..... they are in alphabetical order :)
A : Android
B : -------- no idea, Bug Bite may be
C : Cup Cake
D : Doughnut
E : Ã‰clair
F : Fro yo
G : Gingerbread
H : Honeycomb
I  : Ice cream sandwich (latest one 4.0 API level 14)
F : ???? Jelly, Jack fruit, Justin bieber whatever


In Android only limitation is your imagination, its a game changer.


Humble request to Big Shots(you know them right), pls spare Google from your lawsuits, its not XXXXXsoft, its more of a basic need to humanity, let them do the good work.


Mr. Kapil Sibbal i hope i haven't said anything wrong here to malign someone's image.


Cheers !!