2017-08-07 43 views
1

這是我的GPS代碼。並返回(0,0)作爲位置latlng。如何在返回值不等於(0,0)之前等待幾分鐘。怎麼說GPS等待幾分鐘直到返回不爲零位置

這是我的代碼:

public class Gps implements LocationListener { 
    public static double lon; 
    public static double lat; 
    Location location; 
    @Override 
    public void onLocationChanged(Location location) { 
     this.location=location; 
     lon=location.getLongitude(); 
     lat=location.getLatitude(); 
     } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
    } 
    @Override 
    public void onProviderEnabled(String provider) { 
    } 
    @Override 
    public void onProviderDisabled(String provider) { 
    } 
} 

public String gps() { 
     String str_gps=null; 
     LocationManager lm = null; 
     LocationListener ll = new Gps(); 
     lm = (LocationManager) getSystemService(G.context.LOCATION_SERVICE); 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return null; 
     } 
     boolean gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if (!gpsEnabled) { 
      Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      startActivity(settingsIntent); 
     } 
     gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll); 
     str_gps = "(" + Gps.lat + "," + Gps.lon + ")"; 
     return str_gps; 
    } 

我想使用一個線程,但不知道在哪裏使用它。

+1

爲什麼不使用回調或監聽器,或RxJava,或'LiveData'或事件總線,提供您的位置時的位置可用? – CommonsWare

+0

如何使用監聽器? – sonia

+0

請參閱[本示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/v8.7/Location/Classic),它對位置變化做出反應並顯示天氣預報(注意:預測可能僅適用於在美國)。 – CommonsWare

回答

0

此示例代碼使用計時器檢查gps的有效值。調用的startRecording()用於獲取GPS

private Location getBestLocation() { 
    Location mainLocation = null; 
    Location gpslocation = getLocationByProvider(LocationManager.GPS_PROVIDER); 
    Location networkLocation = 
      getLocationByProvider(LocationManager.NETWORK_PROVIDER); 
    long old = System.currentTimeMillis() - checkInterval * 2; 
    boolean gpsIsOld = true; 
    boolean networkIsOld = true; 


    // if we have only one location available, the choice is easy 
    if (gpslocation == null) { 
     Log.d("gps", "No GPS Location available."); 
     return networkLocation; 
    } 
    if (networkLocation == null) { 
     Log.d("gps", "No Network Location available"); 
     return gpslocation; 
    } 
    if (gpslocation.getTime() < old) { 

     if (networkLocation.getTime() < old) { 
      return null; 
     } 
     return networkLocation; 
    } 


    return gpslocation; 
} 



/** 
* get the last known location from a specific provider (network/gps) 
*/ 
@SuppressWarnings({"ResourceType"}) 
private Location getLocationByProvider(String provider) { 
    Location location = null; 

    LocationManager locationManager = (LocationManager) getActivity().getApplicationContext() 
      .getSystemService(Context.LOCATION_SERVICE); 
    try { 
     if (locationManager.isProviderEnabled(provider)) { 
      location = locationManager.getLastKnownLocation(provider); 
     } 
    } catch (IllegalArgumentException e) { 
     Log.d("gps", "Cannot acces Provider " + provider); 
    } 
    return location; 
} 

//noinspection ResourceType 
@SuppressWarnings({"ResourceType"}) 
public void startRecording() { 
    //gpsTimer.cancel(); 
    gpsTimer = new Timer(); 
    locationManager = (LocationManager) getActivity().getApplicationContext() 
      .getSystemService(Context.LOCATION_SERVICE); 
    for (String s : locationManager.getAllProviders()) { 
     checkGps = false; 
     //startRecording(); 
     locationManager.requestLocationUpdates(s, updateTime, 
       minDistance, new LocationListener() { 

        @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 onLocationChanged(Location location) { 

         doLocationUpdate(location, true); 

        } 
       }); 
     gps_recorder_running = true; 
    } 

    /** 
    if we want to access ui thread 
    */ 


    // start the gps receiver thread 
    timerTask = new TimerTask() { 
     @Override 
     public void run() { 
      Location location = getBestLocation(); 
      if (location != null) { 

       double latitude = location.getLatitude(); 
       double longtitude = location.getLongitude(); 
       SharedPreferencesTools.savelatitude(getContext(), 
         getString(R.string.constant_shared_latitude), "" + latitude); 
       SharedPreferencesTools.saveLongtitude(getContext(), 
         getString(R.string.constant_shared_longtitude), "" + longtitude); 
       Log.i("milliSeconds", "" + date); 
       Log.i("location_shared", "" + location); 
       // 

      } 
     } 
    }; 

    gpsTimer.schedule(timerTask, checkInterval); 
} 

public void doLocationUpdate(Location l, boolean force) { 
    Log.d("gps", "update received:" + l); 
    myLocation = l; 
    if (l == null) { 
     Log.d("gps", "Empty location"); 
     if (force) 
      Toast.makeText(getActivity(), "Current location not available", 
        Toast.LENGTH_SHORT).show(); 
     return; 
    } 
}