Simple GPS

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

Simple GPS

Postby jbrohan » Mon Aug 02, 2010 2:41 pm

This tutorial shows how to get the Longitude and Latitude coordinates from the Android GPS system. 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. For accidental reasons the project is called Hello7.

Notes:-
The LocationManager lm and the LocationListener locationListener are declared in the Activity at lines 14 and 15

In Line 24 the LocationManager lm is connected to the LocationService
in line 26 the requestLocationUpdates requests updates from teh GPS system at such and such a frequencey and if the reading is different from the last by so much. When this happens call locationsListener with the Location.

The rest seems quite straightforward line 41 puts the coordinates into a format suitable for pasting into a browser URL to get a Google map.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.example.hello7;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10.  
  11. public class Hello7 extends Activity {
  12.     /** Called when the activity is first created. */
  13.        
  14.          private LocationManager lm;
  15.          private LocationListener locationListener;
  16.          private double mLongitude = 0;
  17.          private double mLatitude = 0;
  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.                 lm.requestLocationUpdates(
  27.                 LocationManager.GPS_PROVIDER,
  28.                 1000,           // ms since last call
  29.                 10,             // 10 m movement
  30.                 locationListener);      
  31.     }
  32.  
  33.     public class MyLocationListener implements LocationListener
  34.     {
  35.         public void onLocationChanged(Location loc) {                  
  36.                 Log.d("Hello7","MyLocationListener");
  37.              if (loc != null) {
  38.                  Log.d("Hello7","onLocationChanged not null");
  39.                  mLongitude = loc.getLongitude();
  40.                  mLatitude = loc.getLatitude();
  41.                  String s1 = String.format( "http://maps.google.ca/maps?ll=%f,%f",mLatitude,mLongitude);
  42.                  Log.e("Hello7",s1);
  43.             }// loc not null
  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.  
  55.         public void onStatusChanged(String provider, int status, Bundle extras) {
  56.           }
  57.     }
  58.        
  59. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


The Android Manifest for this project is:-
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.hello7"
  4.      android:versionCode="1"
  5.      android:versionName="1.0">
  6.       <uses-permission android:name="android.permission.INTERNET" />
  7.         <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  9.         <activity android:name=".Hello7"
  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: Simple GPS

Postby Maddes » Sun Aug 08, 2010 12:43 am

Hey jbrohan, thanks for that good tutorial! I'm very new to android and I'm experimenting a lot with other code and yours got me thinking and stumpled upon a very basic question I just couldn't find an answer to:
If I want to write an application having only one main acitivity and a couple of java classes for stucturing reasons and I would for instance decide to put your GPS code in one of those classes, I would a least have to create the LocationManager in the main activity and than pass it to the class containing your code, for it to retrieve the GPS signal?

The reason for my question is, that I tried your code in a normal java class, but I got an error on your code in line 24 since there is no getSystemService method, so I figured, that if want to use it GPS, I have to create the LocationManager in an activity and than pass it.
Now I'm not sure if I got this right or if there are much better/more intended ways to structure code (which was my intension for putting your code in a java class in the first place).
Thanks a lot!
Maddes
Freshman
Freshman
 
Posts: 5
Joined: Sat Aug 07, 2010 1:28 pm

Re: Simple GPS

Postby Sivan » Sun Aug 08, 2010 11:50 am

Any Screenshots ???
Sivan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Mon Jul 05, 2010 10:02 am
Location: Bangalore , India

Re: Simple GPS

Postby gregpent » Mon Aug 09, 2010 10:57 am

ok launched the app in the emulator. no errors. it doesn't seem to do anything though so I'm presuming you need to test it on the device itself?
gregpent
Freshman
Freshman
 
Posts: 2
Joined: Mon Aug 09, 2010 10:56 am

Re: Simple GPS

Postby jbrohan » Mon Aug 09, 2010 11:14 am

No. The dalvik (DDMS) includes a gps emulator in the emulator control tab, and you can send new position data to the emulator and get a reaction. There is some uncertainty as I have found that a) it always works once, abd b) it sometimes fails after one or two entries. I'd be happy to find out why.
jbrohan
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Aug 28, 2009 11:12 am
Location: Montreal

Re: Simple GPS

Postby limteckchai » Tue Aug 10, 2010 2:34 am

Hey jbrohan, thanks for the tutorial. Can u please drop in the entire package?? as in the main.xml and any thing else u might have left out .
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Top

Re: Simple GPS

Postby limteckchai » Tue Aug 10, 2010 3:55 am

Hey jbrohan, thanks for the tutorial. I tested this code on the emulator and it works... :P
I just want to confirm with u, and all of you who tested this code that it pushes for an update after every 10 meters of travelling.
Also, what happens if GPS is not on?? i suppose it does not show anything right??
limteckchai
Experienced Developer
Experienced Developer
 
Posts: 92
Joined: Mon Jun 28, 2010 3:33 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests