Get GPS Status on location changed

前端 未结 3 1587
情书的邮戳
情书的邮戳 2020-12-06 23:15

I am using GPS services to derive latitude & longitude in my App. I am able to get the latitude and longitude when the signal is avialable. Once i get a fix, if i have t

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 23:48

    Use NETWORK_PROVIDER instead to get updated location all time. like this..

    public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(Context.LOCATION_SERVICE);
    
                // getting GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
                // getting network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
    //                  locationManager.requestLocationUpdates(
    //                          LocationManager.NETWORK_PROVIDER,
    //                          MIN_TIME_BW_UPDATES,
    //                          MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled){
                        if (location == null){
                            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
    //                      locationManager.requestLocationUpdates(
    //                              LocationManager.GPS_PROVIDER,
    //                              MIN_TIME_BW_UPDATES,
    //                              MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }
    //          if (!isGPSEnabled) {
    //              showSettingsAlert();
    //          } 
            } catch (Exception e){
                e.printStackTrace();
            }
    
            return location;
        }
    

    Hope this helps

提交回复
热议问题