Saturday 16 April 2011

Android - Finding current GPS Coordinates

This is a very simple example to find the current GPS coordinates and altitude. In order to use this example you should turn on your device's GPS.

main.xml
 <?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:id="@+id/latitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/latitude" />  
      <TextView  
           android:id="@+id/longitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/longitude" />  
      <TextView  
           android:id="@+id/altitude_textview"  
           android:layout_width="fill_parent"  
           android:layout_height="wrap_content"  
           android:text="@string/altitude" />  
 </LinearLayout>  

GPSActivity.java
 package com.mobsandgeeks.gps;  
   
 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;  
 import android.widget.Toast;  
   
 public class GPSActivity extends Activity implements LocationListener {  
      // UI References  
      private TextView latitudeTextView;  
      private TextView longitudeTextView;  
      private TextView altitudeTextView;  
   
      private LocationManager locationManager;  
   
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);  
   
           latitudeTextView = (TextView) findViewById(R.id.latitude_textview);  
           longitudeTextView = (TextView) findViewById(R.id.longitude_textview);  
           altitudeTextView = (TextView) findViewById(R.id.altitude_textview);  
   
           locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,  
                     0, this);  
      }  
        
      public void onLocationChanged(Location location) {  
           String latitude = getString(R.string.latitude) + location.getLatitude();  
           String longitude = getString(R.string.longitude) + location.getLongitude();  
           String altitude = getString(R.string.altitude) + location.getAltitude();  
             
           latitudeTextView.setText(latitude);  
           longitudeTextView.setText(longitude);  
           altitudeTextView.setText(altitude);  
      }  
        
      public void onProviderDisabled(String provider) {  
           Toast.makeText(this, "GPS Disabled", Toast.LENGTH_SHORT).show();  
      }  
        
      public void onProviderEnabled(String provider) {  
           Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();  
      }  
        
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
 }  

AndroidManifest.xml
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="com.mobsandgeeks.gps"  
    android:versionCode="1"  
    android:versionName="1.0">  
   <uses-sdk android:minSdkVersion="4" />  
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
   
   <application android:icon="@drawable/icon" android:label="@string/app_name">  
     <activity android:name=".GPSActivity"  
          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>  
 </manifest>  

Source Code
You can download the entire source code from here.

Screenshot

No comments:

Post a Comment