Friday, November 6, 2009

Read GPS location, LocationManager

It's a activity implement LocationListener. When location changed, the method onLocationChanged() will be called-back.


Create a activity AndroidLocation


To let your application to access GPS function, ACCESS_FINE_LOCATION permission is needed. Add the code
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
to AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AndroidLocation"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AndroidLocation"
          android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk android:minSdkVersion="5" />
</manifest>


Modify main.xml to have 2 TextView to show location from GPS.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude:"
/>
<TextView
android:id="@+id/Latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude:"
/>
<TextView
android:id="@+id/Longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Modify the source Code, AndroidLocation.java:
package com.AndroidLocation;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidLocation extends Activity {

private LocationManager myLocationManager;
private LocationListener myLocationListener;
private TextView myLatitude, myLongitude;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myLatitude = (TextView)findViewById(R.id.Latitude);
myLongitude = (TextView)findViewById(R.id.Longitude);

myLocationManager = (LocationManager)getSystemService(
  Context.LOCATION_SERVICE);

myLocationListener = new MyLocationListener();

myLocationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        0,
        0,
        myLocationListener);

//Get the current location in start-up
myLatitude.setText(String.valueOf(
  myLocationManager.getLastKnownLocation(
    LocationManager.GPS_PROVIDER).getLatitude()));

myLongitude.setText(String.valueOf(
 myLocationManager.getLastKnownLocation(
   LocationManager.GPS_PROVIDER).getLongitude()));

}

private class MyLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {
// TODO Auto-generated method stub
myLatitude.setText(String.valueOf(
  argLocation.getLatitude()));
myLongitude.setText(String.valueOf(
  argLocation.getLongitude()));
}

public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}

public void onStatusChanged(String provider,
 int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
}


May be you have to Enable "Use wireless networks" and "Use GPS satellites" in Android Emulator.

And also Change GPS location of Android Emulator, to test the function of the Application.


Because te application read location in start-up, so you have to send GPS Location to Android Emulator at least once before the application start. Edited@11-11-2009


9 comments:

  1. Very interesting post for newbie. I've downloaded your application but getting following error on Emulator.
    "The Application Hello, Android" (process com.AndroidLocation) has stopped unexpectedly. Please try again.

    Any clues?

    ReplyDelete
  2. @raich
    Just comment out the last two lines in onCreate() where the lastknownLocation() is being called and it should work.

    Also, you need to set the permission correctly.

    ReplyDelete
  3. i do the example but i get force close...why?

    ReplyDelete
  4. hello dimitrismidas,

    Because the application read location in start-up, so you have to send GPS Location to Android Emulator at least once before the application start.

    or you can comment the code:
    //Get the current location in start-up
    myLatitude.setText(String.valueOf(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude()));

    myLongitude.setText(String.valueOf(myLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude()));

    ReplyDelete
  5. thanks very much for the detailed instructions. for me, when I open the DDMS perspective in Eclipse, the "set GPS" is deactivated, I cannot enter numbers for lon and lat, neither press "send" button.
    what could be wrong there?

    When I open DDMS directly I can enter data, but it doesn't get received by the app in the emulator (API v8 like my real Samsung i9000).

    What do I do wrong?

    ReplyDelete
  6. hello ths,

    make sure click to select your emulator in Devices windows of DDMS.

    ReplyDelete
  7. hello .. thanks for ur tutorial... i want to download ur app , but cant find in ziddu box file .can u give me app by another link ?

    ReplyDelete
  8. hello nawaf bawazier,

    Sorry, I also haven't the copy of the code.

    ReplyDelete