Android 2.0.1 - GPS locator
Posted 2010-01-10 17:15:06 by thomas
Lately I've spent some time fiddling around with the Android framework. Here is activity code for a GPS locator. Should you get lost somewhere, it can show you your current location.
The application is pieced together based on examples I found on the Internet. Unlike a lot of the sample code on the net, this code is based on the Android 2.0.1 framework
For this to work you need to add "android.permission.ACCESS_FINE_LOCATION" and "android.permission.INTERNET" to your Manifest.xml and to use google maps you need a Api key.
The application is pieced together based on examples I found on the Internet. Unlike a lot of the sample code on the net, this code is based on the Android 2.0.1 framework
public class GPSLocator extends MapActivity {
private LocationManager locationManager;
private LocationProvider locationProvider;
private LocationListener locationListener;
private Location currentLocation;
private MyLocationOverlay myLocationOverlay;
private MapController mapController;
private MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set properties for the mapView
mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(true);
// Set zoom factor
mapController = mapView.getController();
mapController.setZoom(10);
// Get most recent location from built-in location provider
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
currentLocation = locationManager.getLastKnownLocation("gps");
// Get fix updates every 10 sec. or if current location is at least 50 m. from last fix
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
10000, // 10 sec.
50, // meters.
locationListener);
// Call method to draw marker
updateOverlay(currentLocation);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
// Draws a marker on a map overlay and centers the new location
protected void updateOverlay(Location location)
{
// Clear old marker overlay
List overlays = mapView.getOverlays();
if(overlays.size() > 0)
{
for(Iterator it = overlays.iterator(); it.hasNext();)
{
it.next();
it.remove();
}
}
GeoPoint geoPoint = getGeoPoint(location);
// Get marker to draw
Drawable marker = getResources().getDrawable(R.drawable.marker);
marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
// Create new overlay with marker at geoPoint
LocationItemizedOverlay overlay = new LocationItemizedOverlay(marker);
OverlayItem overlayItem = new OverlayItem(geoPoint, "Location", "You are here");
overlay.addOverlay(overlayItem);
// Add overlay to mapView and center to geoPoint
mapView.getOverlays().add(overlay);
mapView.getController().animateTo(geoPoint);
mapView.postInvalidate();
}
// Create a GeoPoint from a Location object
protected GeoPoint getGeoPoint(Location location)
{
if(location == null)
{
return null;
}
Double lat = location.getLatitude()*1E6;
Double lng = location.getLongitude()*1E6;
return new GeoPoint(lat.intValue(), lng.intValue());
}
// Inner Location Listener class definition
protected class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
TextView locationLayout = (TextView)findViewById(R.id.current_location);
locationLayout.setText("Location changed : Lat: " + location.getLatitude() +
" Lng: " + location.getLongitude());
updateOverlay(location);
}
// Omitted empty override methods
}
// Map overlay on which the location marker is drawn
protected class LocationItemizedOverlay extends ItemizedOverlay
{
private List overlays;
public LocationItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
overlays = new ArrayList();
}
@Override
protected OverlayItem createItem(int i) {
return overlays.get(i);
}
@Override
public int size() {
return overlays.size();
}
public void addOverlay(OverlayItem overlay) {
overlays.add(overlay);
populate();
}
}
}
For this to work you need to add "android.permission.ACCESS_FINE_LOCATION" and "android.permission.INTERNET" to your Manifest.xml and to use google maps you need a Api key.