I can't believe this code doesn't work - because I've written several other apps that use location services like this... but apparently I've missed something and I can't figure out what it is. Any help would be greatly appreciated...
My output log check for the tag "GPS" just shows the following:
07-19 05:50:34.567: DEBUG/GPS(4883): Location Services Started
I checked and loading up my maps application or another app that I've written that uses the location API finds me almost immediately - this, on the other hand, leaves me waiting forever without a single update. And yeah - I checked my permissions in AndroidManifest :)
Go figure - I'm completely baffled... Here's the code:
package com.pegshot.pegshotuploader;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import com.pegshot.pegshotuploader.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
public class Map extends MapActivity implements LocationListener {
// Variable Declaration - Global Class Variables
GeoPoint currentLocation = null;
private LocationManager locationManager = null;
// Layout Variables
private MapView mapMapView;
private MapController mapMapController;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Setup the map
mapMapView = (MapView) findViewById(R.id.mapMapView);
mapMapController = mapMapView.getController();
mapMapController.setZoom(4);
// Setup the location services.
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/********************************************
* LOCATION SERVICE FUNCTIONS
*
*/
private void startLocationServices() {
Log.d("GPS","Location Services Started");
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
private void stopLocationServices() {
Log.d("GPS","Location Services Stopped");
if(locationManager != null) {
locationManager.removeUpdates(this);
}
}
@Override
protected void onDestroy() {
stopLocationServices();
super.onDestroy();
}
@Override
protected void onPause() {
stopLocationServices();
super.onPause();
}
@Override
protected void onResume() {
startLocationServices();
super.onResume();
}
/**
* Location Listener Overrides - Handle Location Updates
*/
@Override
public void onLocationChanged(Location location) {
Log.d("GPS",location.toString());
}
@Override
public void onProviderDisabled(String provider) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
// Map Marker Overlay
class RedPegOverlay extends com.google.android.maps.Overlay {
public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when) {
super.draw(canvas, mapView, shadow);
//---translate the GeoPoint to screen pixels---
Point screenPts = new Point();
if(currentLocation != null) {
mapView.getProjection().toPixels(currentLocation, screenPts);
}
//---add the marker---
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.peg_red);
canvas.drawBitmap(bmp, screenPts.x-10, screenPts.y-30, null);
return true;
}
}
}