2017-09-06 100 views
1

我知道周圍有很多東西,但即使我讀了所有(幾乎)我仍然無法弄清楚這個問題。Android獲取用戶位置一次

基本上我想在打開應用程序或按下按鈕並保存共享首選項時獲取用戶FINE LOCATION。 我的舊代碼工作,但由於獲取座標速度很慢,因此無法保存共享首選項(GPS圖標不顯示在欄上)。

OLD:

public void askLocation() { 
    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
     LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     String provider = locationManager.getBestProvider(criteria, true); 
     try { 
      mLocation = locationManager.getLastKnownLocation(provider); 
     } catch (SecurityException e) { 
      // 
     } 

     String currentCoordinates = somename.getString("LastLocation", "0,0"); 

     if (mLocation != null) { 
      double currentLatitude = mLocation.getLatitude(); 
      double currentLongitude = mLocation.getLongitude(); 

      currentCoordinates = currentLatitude + "," + currentLongitude; 
     } 

     SharedPreferences.Editor editor = somename.edit(); 
     editor.putString("LastLocation", currentCoordinates).commit(); 

     Log.d("SomeName", "lastLocation start: " + currentCoordinates); 
    } 
} 

我的新代碼,從#2後使用,沒有得到正確(上欄GPS圖標顯示):

public void askLocation() { 
    if (ContextCompat.checkSelfPermission(MainActivity.this,             Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)   { 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setSpeedRequired(false); 
     criteria.setCostAllowed(true); 
     criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH); 
     criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH); 

     // Now create a location manager 
     LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     Looper looper = null; 
     locationManager.requestSingleUpdate(criteria, locationListener, looper); 

     String currentCoordinates = somename.getString("LastLocation", "0,0"); 
     Log.d("SomeName", "lastLocation askLocation: " + currentCoordinates); 
    } 
} 

final LocationListener locationListener = new LocationListener() { 
    @Override 
    public void onLocationChanged(Location location) { 
     mLocation = location; 
     String currentCoordinates = somename.getString("LastLocation", "0,0"); 

     if (location != null) { 
      double currentLatitude = location.getLatitude(); 
      double currentLongitude = location.getLongitude(); 

      currentCoordinates = currentLatitude + "," + currentLongitude; 
     } 

     SharedPreferences.Editor editor = somename.edit(); 
     editor.putString("LastLocation", currentCoordinates).commit(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     Log.d("Status Changed", String.valueOf(status)); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Log.d("Provider Enabled", provider); 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     Log.d("Provider Disabled", provider); 
    } 
}; 

謝謝。

回答

0

打開應用程序或按下按鈕並保存到共享首選項時,您可以嘗試使用此方法獲取一次位置。

   if (Constants.isOnline(getApplicationContext())) { 
         gps = new GPSTracker(v.getContext(),this); 
         if (gps.canGetLocation()) { 

          latitude = gps.getLatitude(); 
          longitude = gps.getLongitude(); 
          Log.e("Location ", "latitude : " + latitude + "longitude :" + longitude); 

         } else { 
          gps.showSettingsAlert(); 
         } 
       } 

而isOnline()是檢查用戶是否有intenet連接。

public static boolean isOnline(Context c) { 
    ConnectivityManager cm = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
    if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
     return true; 
    } else { 
     Toast.makeText(c, "No Internet Connection.", Toast.LENGTH_SHORT).show(); 
    } 
return false; 

}

+0

'Log.e( 「位置」, 「緯度:」 +緯度+ 「經度:」 +經度); ' 會給你的位置 –

+0

我覺得還有其他的東西缺失嗎?什麼是「GPS」? – FilipeOS

+0

GPSTracker類的一個對象,你可以從[GPSTracker](https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/)找到這個類,你可以有更多的解釋 –