NOTE:

NOTE: Of late, I have been getting requests for very trivial problems that many of you are facing in your day-to-day work. This blog is not to solve your "project" problems - surely not a "Support" site.
I just love to share my knowledge in my spare time and would appreciate any questions or feedback on the articles and code I have shared. I also do appreciate thought-provoking questions that would lead me to write more articles and share.
But please do not put your day-to-day trivial problems here. Even if you do, you most probably would not get a response here.
Thanks

Search This Blog

x

Thursday, 29 October 2009

Location Manager | Android Developer Tutorial (Part 15)


Location-based service is another key functionality that gets used by mobile applications. IT is often combined with maps to give a good user experience. We have already see how to use the external Google Maps API in tutorial Part 14. Here I will build upon the same to show how changes in location can be displayed on the map.
The location services are provided in the android.location package.


In order to use location services, our activity needs to implement the LocationManager Interface.
What does a LocationManager provide?


It is through this interface that an application can access the system’s location services. These services basically allow the application to obtain periodic updates of the device’s geographical location. It also helps in firing an application specific intent when the device comes within the proximity of a specified location.
Since in my example I want to simulate a location change and make my activity respond to the same, I am implementing this interface.


In order to make it an effective application, I have shown my location (hard-coded) on the google map. Then, I have used the location manager to handle any change in the location. The change in location needs to be simulated on the emulator by connecting through telnet. How to simulate location change is given at the end of this tutorial.


Now, dive into the code.


NOTE: I am adding the LocationManager implementation to the same MapActivity class created in the earlier tutorial (Part 14) and hence will not be explaining anything related to the maps API here.


Step 1: Obtain the LocationManager instance:


LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);


After getting the location manager, I am setting default GPS provider and asking to be notified if the location changes by more than 500 meters from the current location, the frequency of updation being 1 sec.


Step 2: Override the onLocationChanged() method in order to respond to changes in location.


      public void onLocationChanged(Location location) {
            if (location != null) {
                  double lat = location.getLatitude();
                  double lng = location.getLongitude();
                  String currentLocation = "The location is changed to Lat: " + lat + " Lng: " + lng;
                  myLoc.setText(currentLocation);
                  geoPoint = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
                  myMC.animateTo(geoPoint);
            }
      }


This method gets invoked when we change the location through the telnet connection to the emulator as described at the end of the tutorial. Based on the latitude and the longitude settings sent, the GeoPoint is changed to display the new location.


That is it. I have not overridden any of the other methods provided by the locationManager. Complete code is downloadable here.



How to simulate location change in the emulator?
Start the Emulator from eclipse
Start a command prompt
Start telnet
Then, type, o localhost 5554
This connects the telnet client to the android emulator (assuming emulator is running locally listening on port 5554. Else this connection fails)
Now type
geo fix 79.000000 13.000000 (or your latitude and longitude)
This sends the location change signal to the emulator.

20 comments:

  1. Hi!,

    Nice info. thanks for sharing.

    ReplyDelete
  2. Hello Sai Geetha,
    this was useful, I want to embed this map in another android app, what I mean to say is; I have developed an app that is built on android 2.2 target and not on google maps.. how do I hook them up?

    ReplyDelete
  3. Hello,very usefull example, but I am running it on my android phone(htc magic)and the map is never shown,any idea why? I am really interested in this kind of application.thanks maxsap

    ReplyDelete
  4. how to get the sea level distance using gps

    ReplyDelete
  5. hi madam,
    i am new to android.i want to build an app its main purpose is to find the user location means that one user having android device & when he goes to a new place,then by using that app he will be knowing the location or place.
    i want to build this app by using location manager.can you please send the source code for finding exact user location.its urgent.

    thanks in adv.

    ReplyDelete
  6. hi madam,

    user have to find his current location without entering latitude & longitude points.

    can u please send the source code.its urgent.
    thanks in adv.

    ReplyDelete
  7. Hello mam,
    i just wanted to ask you one thing. I am working on android 2.0 emulator. i want to know how i can query locationmanager using adb or adb shell to get current geo fix ?
    if there is any other method then please tell me, but i have to use adb shell only. basically is there any way by which i can use adb to get current geo latitude and longitude. can u also tell me where these values stored in emulator like in a database or somewhere else.

    ReplyDelete
  8. Hello Sai,

    I have a problem with this tutorial like the your previous one. The map is not showing up. It's all grids I am seeing.

    Hope you can help me resolve this issue.

    Thanks ang more kudos.

    ReplyDelete
    Replies
    1. Be sure you have entered correct google map API key

      Delete
  9. hi.. Happy Women's Day!! I happened to hit on your blog by searching for Android solution. I felt very interested and proud to know of your achievements and interests. Hatz off..
    - Shwetha

    ReplyDelete
  10. it was very nice and helpful...

    ReplyDelete
  11. hai sai i want to do the gps system in my device i have alreay done the laocation finding by using hard coding but i want to show the locations dynamicaaly plz can u help me

    ReplyDelete
  12. Hai! please help me how to geocode an indian location so that when i enter address in textbox in words it sould return the geocoordinates

    ReplyDelete
  13. Hi Mam,

    I'm new to android applications. Now i want to get the location details in separate class. if customer calls one method like "GetLocation" from main class then it should be called the Locationobj.GetLocation(). but getSystemService throwing exception if i'm extending Activity.
    Please help me mam.

    Thank you.

    ReplyDelete
    Replies
    1. I got a solution,

      I passed the Context from main java class to custom class.

      Thank you.

      Delete