Search This Blog

Monday 16 July 2012

Sample Code - Update Google Map using Location listener

Sample Code - Update Google Map using Location listener

Implement the basic example of google map as explained in the following post.


This post will focus on implementing the location listener and update the location in google map.

You need to add the following code for the location listener in the class extending the MapActivity class (see the GMapSample class from the previous sample)

private final LocationListener listener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            // A new location update is received.  Do something useful with it.  
             Log.i(TAG,"Location Listener start");
             String coordinates[] = {""+location.getLatitude(), ""+location.getLongitude()};
                double lat = Double.parseDouble(coordinates[0]);
                double lng = Double.parseDouble(coordinates[1]);
                GeoPoint p = new GeoPoint(
                (int) (lat * 1E6),
                (int) (lng * 1E6));
                mapController.animateTo(p);
                mapController.setZoom(7);
                mapView.invalidate();     

        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.i(TAG,"Location disable");
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.i(TAG,"Location enabldes");
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };

Now add a location manager object in class as follows


private LocationManager locMgr;


Add the following lines of highlighted code in the onCreate Method to register the location listener with location manager.



@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gsample);
        mapView = (MapView) findViewById(R.id.map_view);      
        mapView.setBuiltInZoomControls(true);
        locMgr = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        //register the location listener with location manager       
        locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);

        mapController = mapView.getController();
        mapController.animateTo(point);
        mapController.setZoom(7);
        mapView.invalidate();     
                          
    }

The eclipse will not automatically add the import required for location APIs


import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;


Now when you run the application you can see the following output.








To send the location data to the emulator for testing you can use the DDMS tool


DDMS Tool Android
DDMS Tool

select the emulator device and go to the Emulator Control. See the Location Controls and update the location data and click send to send the location data to the emulator running the application.


If you like the post then please provide your feedback.


Thanks
Creative Android Apps

12 comments:

  1. Last month, the California-based internet giant began re-evaluating its user-edited online map system after the latest embarrassing incident -- an image of an Android mascot urinating on an Apple logo. To know more about , Visit Android training in chennai

    ReplyDelete
  2. It would have been the happiest moment for you,I mean if we have been waiting for something to happen and when it happens we forgot all hardwork and wait for getting that happened.
    Java interview questions and answers

    Core Java interview questions and answers

    Java training in Chennai | Java training in Tambaram

    Java training in Chennai | Java training in Velachery

    ReplyDelete
  3. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
    excel advanced excel training in bangalore
    Devops Training in Chennai

    ReplyDelete
  4. I am a regular reader of your blog and being students it is great to read that your responsibilities have not prevented you from continuing your study and other activities. Love
    angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs Training in chennai

    angularjs-Training in pune

    angularjs-Training in chennai

    ReplyDelete
  5. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    microsoft azure training in bangalore
    rpa training in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. I absolutely love your site.. Very nice colors & theme. Did you develop this amazing site yourself? Please reply back as I’m wanting to create my own personal website and would love to know where you got this from or what the theme is named. Thanks
    UI Development Training In Marathahalli
    Selenium Training In Bangalore

    ReplyDelete
  8. Very informative article. I really loved the way content is written here. I have also shared this article on my social media channels.

    inbound marketing agency in india

    outsource website development

    top digital marketing agencies in india

    virtual assistant websites

    ReplyDelete