Get Location problem

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Get Location problem

Postby Steph » Fri Oct 08, 2010 11:12 pm

Hi every one,

I try to make a simple app which display the location of the user.
To do that, i use locationListener, but i don't know why, the onLocationChanged is never call, so i have no location.

I give you my simple code, if you have any idea...

thanks in advance!

PS: manifest are correct

Code: Select all
package stephane.castrec.main;

import java.util.List;

import stephane.castrec.Util.myOverlay;

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;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;


public class Carte extends MapActivity{

   private MapView _MapView;
   private LocationManager _locMgr;
   private MyLocationOverlay _LocOverlay;
   private GeoPoint _CurrentLocationPoint = null;
   private ProgressDialog _dialog = null;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
       try
       {
           super.onCreate(savedInstanceState);
           Log.d("CarLost", "starting carte");
           locationUpdate(this);
           setContentView(R.layout.carte);
           _MapView = (MapView) findViewById(R.id.mapview);
           _MapView.setBuiltInZoomControls(true);
          
                
   
           _LocOverlay = new MyLocationOverlay(getApplicationContext(), _MapView);
           _LocOverlay.enableMyLocation();

           Log.d("CarLost", "Carte create GPS");
           MyGps gps = new MyGps();
           if(_CurrentLocationPoint == null)
           {
              Log.d("CarLost", "No location set");
                _dialog = ProgressDialog.show(this, "",
                           "Loading. Please wait...", true);
           }
           else
           {
              Log.d("CarLost", "my loc "+_CurrentLocationPoint);
              displayLocationOnMap();
           }
       }
       catch (Exception e) {
          Log.e("CarLost", "carte constructor error : "+e.getMessage());
      }
    }   
   
    private void displayLocationOnMap()
    {
       try
       {
          List<Overlay> mapOverlays = _MapView.getOverlays();
           Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
           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", "Carte displayLocationOnMap exception "+e.getMessage());
      }
    }
   
   @Override
   protected boolean isRouteDisplayed() {
      // TODO Auto-generated method stub
      return false;
   }
   
   public void locationUpdate(Context pContext)
    {
        Log.d("CarLost", "locationUpdate starting");
       try
       {
           //Get My current Location
           LocationManager mlocManager = (LocationManager)pContext.getSystemService(Context.LOCATION_SERVICE);
           LocationListener mlocListener = new MyGps();
           Log.d("CarLost", "starting my gps created");
          
           Criteria cr = new Criteria();
           cr.setPowerRequirement(Criteria.POWER_LOW);
           cr.setAccuracy(Criteria.ACCURACY_FINE);

           mlocManager.requestLocationUpdates( mlocManager.getBestProvider(cr,true), 10, 2, mlocListener);
           Location lLoc = mlocManager.getLastKnownLocation(mlocManager.getBestProvider(cr,true));
           if(!lLoc.equals(null))
           {
              Log.d("CarLost", "location update getLastKnownLocation OK ");
              _CurrentLocationPoint = new GeoPoint((int)lLoc.getLatitude(), (int)lLoc.getLongitude());
           }
           Log.d("CarLost", "location update finish");
       }
       catch (Exception e) {
          Log.e("CarLost", "getLocation error "+e.getMessage());
       }     
    }
   
   public class MyGps implements LocationListener {
      /* Class My Location Listener */
      
      @Override
      public void onLocationChanged(Location Loc) {
         Log.d("CarLost","onLocationChanged "+Loc.toString());
         if(_dialog.isShowing())
         {
            _dialog.dismiss();
            _CurrentLocationPoint = new GeoPoint((int)Loc.getLatitude(), (int)Loc.getLongitude());
         }
      }

      @Override
      public void onProviderDisabled(String provider) {         
      }

      @Override
      public void onProviderEnabled(String provider) {
      }

      @Override
      public void onStatusChanged(String provider, int status, Bundle extras) {
      }
   }
}

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

Top

Re: Get Location problem

Postby J_R » Sun Oct 10, 2010 2:37 pm

The only thing that is jumping out at me is that you've got pretty restrictive criteria POWER_LOW and ACCURACY_FINE... still I would imagine it would be able to find a listener.

Have you tried running this in the emulator and sending through location updates with DDMS?
http://www.staircase3.com
J_R
Developer
Developer
 
Posts: 32
Joined: Fri Jan 22, 2010 12:37 am

Re: Get Location problem

Postby Steph » Sun Oct 10, 2010 2:41 pm

I work on my Htc Desire!
I test my application inside my apartment, maybe that's the point!
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 5 guests