Location returned is null when provider is gps?

前端 未结 3 911
你的背包
你的背包 2020-12-11 08:50

I am trying to retrieve the my current location coordinates in the following piece of code

LocationManager locationManager= (LocationManager) getSystemServic         


        
相关标签:
3条回答
  • 2020-12-11 09:10

    I think the problem is that GPS take a fiew second to syncronice. You must to use a thread. Take this example:

    @Override
    public void run() {
    
        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
    
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Looper.prepare();
            mToast.Make(getContext(),"GPS",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
            Looper.loop(); 
            Looper.myLooper().quit(); 
    
        } else if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
            Looper.prepare();
            mToast.Make(getContext(),"Triangulacion",0);
            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
            Looper.loop();
            Looper.myLooper().quit();
        }else{
            mToast.Make(context,"No se encuentra señal se procede a mandar un mensaje normal",0);
            Looper.prepare();
            handlerNormal.sendEmptyMessage(0);
            Looper.loop();
            Looper.myLooper().quit();
        }   
    
    }
    

    and a Location Listener

    private class MyLocationListener implements LocationListener 
    {
        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                setCurrentLocation(loc);
                handler.sendEmptyMessage(0);
            }
        }  
    

    //...
    }

    0 讨论(0)
  • 2020-12-11 09:11

    It takes a while for the GPS provider to get a fix, especially if no previous fix was made. The getLastKnownLocation is not a blocking call that will wait untill an actual live GPS fix is established (that fix can take up to 1 min, depending on various reasons).

    Also, make sure you're outside for obvious reasons).

    Instead of simply calling the getLastKnownLocation for the GPS provider, you first need to request location updates from it.

    The following article explains the principle very well: http://developer.android.com/guide/topics/location/obtaining-user-location.html

    It's a must read for anyone doing anything with the location provider on Android.

    the typical flow is:

    1. Start application.
    2. Start listening for updates from desired location providers.
    3. Maintain a "current best estimate" of location by filtering out new, but less accurate fixes.
    4. Stop listening for location updates.
    5. Take advantage of the last best location estimate.

    Looking at the code below, we’re initializing a MyLocationListener (to track the phone’s position), and retrieving a reference to the LocationManager. We’re requesting location updates from the locationmanager. We’re using a minDistance (minimum distance interval for notifications) of 10 meters, and a minTime (the minimum time interval for notifications) of 35 seconds.

    LocationListener locationListener = new MyLocationListener();
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
    

    When implementing this, you'll probably notice that the MyLocationListener receives more updates than you expected (given the minTime and minDistance parameters). The following article at http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/ can help you understand why.

    0 讨论(0)
  • 2020-12-11 09:16

    It takes some seconds until the mobile connects to some satellites. As long as the phone has no connection, because the connection is not yet established, the location will be null.

    You have to request location updates from the GPS provider too.

    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    
    0 讨论(0)
提交回复
热议问题