2017-09-06 73 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/)找到这个类,你可以有更多的解释 –