Pages

Monday, August 15, 2011

Location Manager

Basic description of algorithm in step by step form:
1.) Create a Project LocManageDemo.
2.) Add the following permissions to your application in AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.app.LocManageDemo" android:versionCode="1"
   android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <uses-library android:name="com.google.android.maps" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET">
    </uses-permission>
       
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LocManageDemo" android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
        </activity>
    <uses-library android:required="true" android:name="com.google.android.maps">
    </uses-library>
    </application>
</manifest>
3.) Put the following code in res/layout/main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/mainlayout"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >     <com.google.android.maps.MapView
       android:id="@+id/mapview"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:clickable="true"
       android:apiKey="Your Maps API Key"
   />
</RelativeLayout>
4.) Replace “Your Maps API Key” with your Google API key.
5.) The next step requires that you have to generate your Google Map key. Steps to generate Google Map Key :
i) Run your cmd prompt.
ii) Reach your folder where you have installed java because you have to use a tool called keytool.exe located here.
Example :C:/Program Files/Java/jdk1.6.0_22/bin/java
iii) Enter the following in cmd prompt.
keytool.exe -list -alias androiddebugkey -keystore C:\myandroid\.android\debug.keystore -storepass android -keypass android
iv) You will get a MD5 fingerprint something like the text below
94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
v) Copy the MD5 fingerprint and put it here
http://code.google.com/android/maps-api-signup.html
vi) You will get a Google API key something like this :
0cgwXB2sO7JhRnX4YhkuytZQCxLgh6hOxUbu01g0cgwXB2sO7JhRnX4YhkuytZQCxLgh6hOxUbu01g
6.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project. Give the respective project name i.e. LocManageDemo. Enter following information:
Project name: LocManageDemo
Build Target: Google APIs
Application name: LocManageDemo
Package name: com.app.LocManageDemo
Create Activity: LocManageDemo
Make sure to select the “Google API” as Target.

On Clicking Finish LocManageDemo code structure is generated with the necessary Android Packages being imported along with LocManageDemo.java. LocManageDemo class will look like following:
package com.app.LocManageDemo; import android.os.Bundle;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.RelativeLayout;
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 LocManageDemo extends MapActivity
{
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.main); // bind the layout to the activity
        // create a map view
        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setStreetView(true);
        mapController = mapView.getController();
        mapController.setZoom(14); // Zoom 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());
   }
   @Override
   protected boolean isRouteDisplayed() {
        return false;
   }
   public class GeoUpdateHandler implements LocationListener {
     @Override
     public void onLocationChanged(Location location) {
        int lat = (int) (location.getLatitude() * 1E6);
        int lng = (int) (location.getLongitude() * 1E6);
        GeoPoint point = new GeoPoint(lat, lng);
        mapController.animateTo(point);
     }
     @Override
     public void onProviderDisabled(String provider) { }
     @Override
     public void onProviderEnabled(String provider) { }
     @Override
     public void onStatusChanged(String provider, int status, Bundle extras) { }
  }
}
Output –The final output:

No comments:

Post a Comment

Popular Posts