Android GPS Example
April 11th, 2010
For some reason there doesn’t seem to be a clear example showing how to use GPS on Android. Here is some code I have written that listens for location changes and displays the data in a TextView. The example also shows how to open the GPS setting with code via an Intent, since sometimes the GPS will be turned off by the user.
Code
package uk.co.hejp.GPSExample;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity implements LocationListener {
/* this class implements LocationListener, which listens for both
* changes in the location of the device and changes in the status
* of the GPS system.
* */
static final String tag = "Main"; // for Log
TextView txtInfo;
LocationManager lm;
StringBuilder sb;
int noOfFixes = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* get TextView to display the GPS data */
txtInfo = (TextView) findViewById(R.id.textInfo);
/* the location manager is the most vital part it allows access
* to location and GPS status services */
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected void onResume() {
/*
* onResume is is always called after onStart, even if the app hasn't been
* paused
*
* add location listener and request updates every 1000ms or 10m
*/
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f, this);
super.onResume();
}
@Override
protected void onPause() {
/* GPS, as it turns out, consumes battery like crazy */
lm.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
Log.v(tag, "Location Changed");
sb = new StringBuilder(512);
noOfFixes++;
/* display some of the data in the TextView */
sb.append("No. of Fixes: ");
sb.append(noOfFixes);
sb.append('\n');
sb.append('\n');
sb.append("Londitude: ");
sb.append(location.getLongitude());
sb.append('\n');
sb.append("Latitude: ");
sb.append(location.getLatitude());
sb.append('\n');
sb.append("Altitiude: ");
sb.append(location.getAltitude());
sb.append('\n');
sb.append("Accuracy: ");
sb.append(location.getAccuracy());
sb.append('\n');
sb.append("Timestamp: ");
sb.append(location.getTime());
sb.append('\n');
txtInfo.setText(sb.toString());
}
@Override
public void onProviderDisabled(String provider) {
/* this is called if/when the GPS is disabled in settings */
Log.v(tag, "Disabled");
/* bring up the GPS settings */
Intent intent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
@Override
public void onProviderEnabled(String provider) {
Log.v(tag, "Enabled");
Toast.makeText(this, "GPS Enabled", Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
/* This is called when the GPS status alters */
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
Log.v(tag, "Status Changed: Out of Service");
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
Log.v(tag, "Status Changed: Temporarily Unavailable");
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
Log.v(tag, "Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
protected void onStop() {
/* may as well just finish since saving the state is not important for this toy app */
finish();
super.onStop();
}
}
Permissions
Of course the Android framework has security permissions to protect the user. To get the permission to use GPS add a uses-permission tag with attribute android:name set to “android.permission.ACCESS_FINE_LOCATION” to the AndroidManifest.xml document as a direct child of the manifest tag, i.e:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />Notes When testing this code in the emulator, you can send mock fixes by connecting to the emulator using telnet, i.e. ‘telnet localhost 5554′ and typing: geo fix -10.223, 40.549
PerlMagick Example
July 6th, 2009
PerlMagick is a perl binding for ImageMagick. It’s useful for writing perl scripts that manipulate or outright create images. Here’s a little example of how to use it:
use Image::Magick;
# PerlMagick example script
# Written by Howard Paget
#creates a new ImageMagick object
$q = Image::Magick->new(size=>'500x150');
#sets the background to green
$q->Read('xc:#91FF23');
#draws some ellipses (circles) with such and such attributes
$q->Draw(primitive=>'Ellipse',stroke=>'#1a1a1a',fill=>'#2A5CFF',strokewidth=>3,
points=>'250,75 60,60 0,360');
$q->Draw(primitive=>'Ellipse',stroke=>'#1a1a1a',fill=>'#fff',strokewidth=>3,
points=>'250,75 40,40 0,360');
$q->Draw(primitive=>'Ellipse',stroke=>'#1a1a1a',fill=>'#f00',strokewidth=>3,
points=>'250,75 20,20 0,360');
#outputs the object to hello.png
print $q->Write('hello.png');
Output:
Here’s some handy information about the library.
Here’s the script that created the title image.
Notes:
The ‘points’ variable in the draw function doesn’t necessaryly contain points as such, for example the the ‘points’ variable for the ellipse primitive means ‘x,y width,height startAngle,endAngle’.
Preamble
This ‘how to’ uses the NetworkManager Applet, which is installed by default on Ubuntu. In the example a wireless internet connection (wlan0) is shared with a wired (eth0) machine/device (e.g. Xbox 360, other PC, switch, etc).
Steps
1. Right click on the NetworkManager applet in the system tray, and select ‘Edit Connections…’ from the menu.
2. Select the ‘Wired’ tab, select correct connection typically some variant of ‘eth0′.
3. Click the ‘Edit…’ button to the right, you’ll likely be asked for your password after clicking this.
4. Select the IPv4 Settings.
5. Change ‘Method’ to ‘Shared to other computers’.
There we go now your Xbox 360, other PC, etc should be able to connect to the internet.
2. Select the ‘Wired’ tab, select correct connection typically some variant of ‘eth0′.
3. Click the ‘Edit…’ button to the right, you’ll likely be asked for your password after clicking this.
4. Select the IPv4 Settings.
5. Change ‘Method’ to ‘Shared to other computers’.
There we go now your Xbox 360, other PC, etc should be able to connect to the internet.