Search This Blog

Showing posts with label Google Map. Show all posts
Showing posts with label Google Map. Show all posts

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

Sunday, 15 July 2012

Google Map in Android Application - Sample Code


Sample Code - Google Map in Android Application


Location aware applications on android platform needs an intuitive way to present the location information.
Google provides a set of APIs to integrate the Google Map for development of android applications and bundled as Google APIs.


This post is focusing on Google Map object integration with android application.


Create a Android application project "GMapSample" and open the default class file generated in the src directory as the main activity class. See here the project creation dialog


Eclipse Android Project
Create New Android Application Project Using Eclipse
To create the Google Map in the application you need to select the Build SDK as Google APIs (API15). If this is not available then you need to update the SDK and download using Android SDK Manager.


The following includes need to add in order to integrate the Google Map with your application.


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


The MapActivity extends the basic Activity class of Android and provides the Google Map as the user interface.


In order to access the Google Map in your application you need to get a Application Key and define the Google Map object in your application XML layout.


 <com.google.android.maps.MapView
                 android:id="@+id/map_view"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:apiKey="0HvaRYYCBNDap4SKY0HN-XXXXXXXXXXXXXXXX"
                 />


To get the API key for your application see the following link.


http://creativeandroidapps.blogspot.in/2012/07/get-application-key-for-google-map.html


Now replace the code of main activity class by following code


package com.example.gmapsample;

import android.os.Bundle;

import android.view.Menu;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;


public class GMapSample extends MapActivity{

    private static final String TAG = "GMapSample";
    private MapView mapView;
    private MapController mapController;
    private static final int latitudeE6 = 17421306;
    private static final int longitudeE6 = 78457553;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gsample);
        mapView = (MapView) findViewById(R.id.map_view);   //get the map view resource added in XML file   
        mapView.setBuiltInZoomControls(true);
        GeoPoint point = new GeoPoint(latitudeE6, longitudeE6); //defines the geo location
        mapController = mapView.getController(); //get the map controller
        mapController.animateTo(point); // show the location defined by geopoint
        mapController.setZoom(7);  //setting the map zoom level
        mapView.invalidate();  //redraw the map   
                          
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_gsample, menu);
        return true;
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}


while compiling the application you will get some error because we have not added the use of google APIs in AndroidManifest.xml file of the application.


Add the following line in your application manifest file.


<uses-library android:name="com.google.android.maps" />


Add the following permissions also to manifest file.



<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>




To run the application you need to define a AVD from the AVD Manager as follows


AVD Manager
AVD for Google Map


Running the application will give the following output.


Google Map Android
Android Application with Google Map
Please provide your feedback. The next post for the Google Map is on the way.


Thanks
Creative Android Apps