Gps not working

Tutorials that use the MapActivity. Many using GPS functionality.

Gps not working

Postby Steph » Sun Feb 06, 2011 10:28 pm

Hi everybody,

I try to make a simple app that can display my location. But when i try to use GPS i have not onLocationChanged.
I really don't know why because when i use the NETWORK_PROVIDER. So, i really don't understand.
If any of you has an idea, please share it ;)

Thanks in advance
Code: Select all
package stephane.castrec.main;

import java.util.List;

import stephane.castrec.Util.myOverlay;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class Map extends MapActivity {

   final private String PREF_LONG = "Longitude";
   final private String PREF_LAT = "Latitude";
   static public String PREFS_NAME = "CarLostPref";

   private Boolean _isFindMap = false;//false = set location; true = retrieve location

   private MapView _MapView;
   private MyLocationOverlay _LocOverlay;
   private GeoPoint _CurrentLocationPoint = null;
   private GeoPoint _CarLocationPoint = null;
   private ProgressDialog _dialog = null;
   LocationManager _locationManager;

   private int CONVERSION_FACTOR = 1000000;
   private double LATITUDE_MIN = -80;
   private double LATITUDE_MAX = 80;
   private double LONGITUDE_MIN = -80;
   private double LONGITUDE_MAX = 80;

   private Handler _Handler = new Handler();
   final Runnable _UpdateResults = new Runnable() {
      public void run() {
         updateUi();
      }
   };
   
   //update location
   LocationListener _LocationListener = new LocationListener() {            
      @Override
      public void onLocationChanged(Location Loc) {
         _CurrentLocationPoint = new GeoPoint(clipLatitude((int)(Loc.getLatitude()*CONVERSION_FACTOR)), clipLongitude((int)(Loc.getLongitude()*CONVERSION_FACTOR)));
         _Handler.post(_UpdateResults);
         _locationManager.removeUpdates(this);//disable other update
      }
      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {}            
      @Override
      public void onProviderEnabled(String provider) {}            
      @Override
      public void onProviderDisabled(String provider) {}            
   };

   @Override
   public void onCreate (Bundle savedInstanceState)
   {
      try
      {
         super.onCreate(savedInstanceState);
         Bundle extras =this.getIntent().getExtras();
         if(extras !=null)
            _isFindMap = extras.getBoolean("Choice");
         if(_isFindMap)
            retrieveCarLocation();
         setContentView(R.layout.carte);
         _MapView = (MapView) findViewById(R.id.mapview);
         _MapView.setBuiltInZoomControls(true);

         _LocOverlay = new MyLocationOverlay(getApplicationContext(), _MapView);
         _LocOverlay.enableMyLocation();

         _dialog = ProgressDialog.show(this, "",
               "Loading. Please wait...", true);//TODO language

         //do the loc request
         _locationManager = (LocationManager) this.getSystemService(this.LOCATION_SERVICE);
         boolean lUseGps=_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

         //do update location request
         if(lUseGps)
         {
            _locationManager.requestLocationUpdates(
                  LocationManager.GPS_PROVIDER, 0, 0, _LocationListener);
            Log.i("CarLost","onCreate use gps");
         }
         else
         {
            _locationManager.requestLocationUpdates(
                  LocationManager.NETWORK_PROVIDER, 0, 0, _LocationListener);
            Log.i("CarLost","onCreate use network");
         }
      }
      catch (Exception e) {
         Log.e("CarLost", "Map onCreate error "+e.getMessage());
      }
   }

   private void updateUi()
   {
      try
      {
         _dialog.dismiss();
         if(!_isFindMap)
         {
            displayLocationOnMap(false);
            displayCarOnMap();
         }
         else
            displayLocationOnMap(true);
      }
      catch (Exception e) {
         Log.e("CarLost", "Map updateUi error : "+e.getMessage());
      }
   }

   private void displayCarOnMap()
   {
      try
      {
         Log.d("CarLost", "displayCarOnMap starting");
         List<Overlay> mapOverlays = _MapView.getOverlays();
         Drawable drawable = this.getResources().getDrawable(R.drawable.car);
         myOverlay itemizedoverlay = new myOverlay(drawable); 
         OverlayItem overlayitem1 = new OverlayItem(_CarLocationPoint, "My Car", "parked here");
         itemizedoverlay.addOverlay(overlayitem1);
         mapOverlays.add(itemizedoverlay);
         _MapView.getController().setCenter(_CarLocationPoint);         
      }
      catch (Exception e) {
         Log.d("CarLost", "Map displayCarOnMap exception "+e.getMessage());
      }
   }

   /**
    *
    * @param pCar: if true display car
    *             else display people
    */
   private void displayLocationOnMap(Boolean pCar)
   {
      try
      {
         Log.d("CarLost", "displayLocationOnMap starting");
         List<Overlay> mapOverlays = _MapView.getOverlays();
         Drawable drawable;
         if(pCar)
            drawable = this.getResources().getDrawable(R.drawable.car);
         else
            drawable = this.getResources().getDrawable(R.drawable.point);
         myOverlay itemizedoverlay = new myOverlay(drawable); 
         OverlayItem overlayitem1 = new OverlayItem(_CurrentLocationPoint, "My Car", "parked here");
         itemizedoverlay.addOverlay(overlayitem1);
         mapOverlays.add(itemizedoverlay);
      }
      catch (Exception e) {
         Log.d("CarLost", "Map displayLocationOnMap exception "+e.getMessage());
      }
   }

   private void retrieveCarLocation()
   {
      try
      {
         SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
         _CarLocationPoint = new GeoPoint(settings.getInt(PREF_LAT, -1000), settings.getInt(PREF_LONG, -1000));
         if(_CarLocationPoint.getLatitudeE6()==-1000 && _CarLocationPoint.getLongitudeE6()==-1000)
            _CarLocationPoint = null;//error
      }
      catch (Exception e) {
         Log.e("CarLost", "retrieveCarLocation exeption "+e.getMessage());
      }
   }

   @Override
   protected boolean isRouteDisplayed() {
      return false;
   }

   private int clipLatitude(int latitude) {
      if (latitude < LATITUDE_MIN * CONVERSION_FACTOR) {
         return (int) (LATITUDE_MIN * CONVERSION_FACTOR);
      } else if (latitude > LATITUDE_MAX * CONVERSION_FACTOR) {
         return (int) (LATITUDE_MAX * CONVERSION_FACTOR);
      } else {
         return latitude;
      }
   }

   private int clipLongitude(int longitude) {
      if (longitude < LONGITUDE_MIN * CONVERSION_FACTOR) {
         return (int) (LONGITUDE_MIN * CONVERSION_FACTOR);
      } else if (longitude > LONGITUDE_MAX * CONVERSION_FACTOR) {
         return (int) (LONGITUDE_MAX * CONVERSION_FACTOR);
      } else {
         return longitude;
      }
   }
}

Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to Map Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests