GPS and Network Location

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

GPS and Network Location

Postby jbrohan » Mon Aug 02, 2010 3:01 pm

This tutorial shows how to get the Longitude and Latitude coordinates from the Android GPS system, and also from the Networkl Locationm using the broadcasting towers.. It uses the DDMS debugger system to display results (This is a page in the Eclipse or a separate program run from a command prompt in Windows). For simplicity and clarity there is no front panel and extraneous code is kept to a minimum. Note the uses-permission in the Android Manifest

The LocationManager lm and the LocationListener locationListener are declared in the Activity at lines 16-17
The locationListener is linked to the GPS_PROVIDER and the NETWORK_PROVIDER with a period and a range of movement. lines 27 to 37.

onLocationChanged calls the private function getGPS().


getGPS() gets a list of the location providers, and then loops through them displaying some information about them.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.location;
  2.  
  3. import java.util.List;
  4.  
  5. import android.app.Activity;
  6. import android.content.Context;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12.  
  13. public class LocationServices extends Activity {
  14.    
  15.        
  16.         LocationManager lm;
  17.         private LocationListener locationListener;
  18.        
  19.     @Override
  20.     public void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.main);
  23.        
  24.         lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
  25.         locationListener = new MyLocationListener();
  26.        
  27.         lm.requestLocationUpdates(
  28.                 LocationManager.GPS_PROVIDER,
  29.                 3000,           // ms since last call
  30.                 0,              //  m movement
  31.                 locationListener);    
  32.         lm.requestLocationUpdates(
  33.                 LocationManager.NETWORK_PROVIDER,
  34.                 200,            // ms since last call
  35.                 0,              //  m movement
  36.                 locationListener);  
  37.        
  38.     }
  39.    
  40.     public class MyLocationListener implements LocationListener
  41.     {
  42.         public void onLocationChanged(Location loc) {  
  43.                 double coordinates[] = getGPS();               
  44.         }
  45.        
  46.             public void onProviderDisabled(String provider) {
  47.                 // TODO Auto-generated method stub
  48.             }
  49.        
  50.             public void onProviderEnabled(String provider) {
  51.                 // TODO Auto-generated method stub
  52.                 Log.d("Hello7","onProviderEnabled");
  53.             }  
  54.             public void onStatusChanged(String provider, int status, Bundle extras) {
  55.             }
  56.     }
  57.     onLocationChanged calls getGPS()
  58.     private double[] getGPS() {
  59.        
  60.         List<String> providers = lm.getProviders(true);
  61.         double[] gps = new double[2];
  62.         /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
  63.         Location l = null;
  64.        
  65.         for (int i=providers.size()-1; i>=0; i--) {
  66.                 String s = providers.get(i);
  67.                 Log.d("LocServ",String.format("provider (%d) is %s",i,s));
  68.                 l = lm.getLastKnownLocation(providers.get(i));  
  69.                 if (l != null) {
  70.                         gps[0] = l.getLatitude();
  71.                         gps[1] = l.getLongitude();
  72.                         Log.d("LocServ",String.format("Lat %f, Long %f accuracy=%f",gps[0],gps[1],l.getAccuracy()));
  73.                 }      
  74.         }
  75.         return gps;
  76.     }
  77. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.      package="com.example.location"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.       <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  7.       <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  9.         <activity android:name=".LocationServices"
  10.                  android:label="@string/app_name">
  11.             <intent-filter>
  12.                 <action android:name="android.intent.action.MAIN" />
  13.                 <category android:name="android.intent.category.LAUNCHER" />
  14.             </intent-filter>
  15.         </activity>
  16.  
  17.     </application>
  18.     <uses-sdk android:minSdkVersion="3" />
  19.  
  20. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
jbrohan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal

Top

Re: GPS and Network Location

Postby jbrohan » Wed Aug 04, 2010 8:52 pm

error line 57 should be acomment!!! sorry
jbrohan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests