2017-02-20 62 views
0

所以我想顯示我的位置的經緯度,但是隻有當我再次啓動應用程序時纔會顯示我的位置。 當我安裝應用程序,並第一次啓動它沒有收到位置。 什麼是第一次獲得當前位置的簡單方法?我做錯了什麼?Google位置服務getLastLocation在第一次啓動應用程序時爲空

private final static String LOG_TAG = MainActivity.class.getName(); 
private GoogleApiClient googleApiClient; 
private Location lastLocation; 
private LocationRequest locationRequest; 
private TextView locationLatitude; 
private TextView locationLongitude; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    locationLatitude = (TextView) findViewById(R.id.latitude_text); 
    locationLongitude = (TextView) findViewById(R.id.longitude_text); 

    buildGoogleApiClient(); 
} 

@Override 
protected void onStart() { 
    super.onStart(); 
    googleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 
    googleApiClient.disconnect(); 
} 

private void buildGoogleApiClient() { 
    googleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Log.d(LOG_TAG, "onConnected"); 
    locationRequest = LocationRequest.create(); 
    locationRequest.setInterval(1000); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 

    if(Build.VERSION.SDK_INT < 21){ 
     lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    } 
    else { 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
     } 
     else{ 
      lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     } 
    } 


    if(lastLocation != null){ 
     locationLatitude.setText(String.valueOf(lastLocation.getLatitude())); 
     locationLongitude.setText(String.valueOf(lastLocation.getLongitude())); 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    if(requestCode == 1){ 
     if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ 
      if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
       lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
      } 
     } 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 
    Log.d(LOG_TAG, "onConnectionSuspended"); 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
    Log.d(LOG_TAG, "onConnectionFailed"); 
} 

@Override 
public void onLocationChanged(Location location) { 
    Log.d(LOG_TAG, "onLocationChanged"); 
    locationLatitude.setText(String.valueOf(location.getLatitude())); 
    locationLongitude.setText(String.valueOf(location.getLongitude())); 
} 
+0

有時候最後的位置不可用,並會爲空。考慮將最後一個位置保存在此場景的首選項中,或者返回到第一次下載應用程序時所遵循的過程。 – danny117

回答

0

你永遠不要求位置更新:我在仿真器API 24

這裏是我的代碼運行此。

試試這個:

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    Log.d(LOG_TAG, "onConnected"); 
    locationRequest = LocationRequest.create(); 
    locationRequest.setInterval(1000); 
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
LocationServices.FusedLocationApi.requestLocationUpdates(
      googleApiClient, locationRequest, this); 


    if(Build.VERSION.SDK_INT < 21){ 
     lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    } 
    else { 
     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1); 
     } 
     else{ 
      lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
     } 
    } 


    if(lastLocation != null){ 
     locationLatitude.setText(String.valueOf(lastLocation.getLatitude())); 
     locationLongitude.setText(String.valueOf(lastLocation.getLongitude())); 
    } 
} 
+0

我已將我的onConnected方法更改爲您的,但現在應用程序崩潰,這是異常':java.lang.SecurityException:客戶端必須具有ACCESS_FINE_LOCATION權限才能請求PRIORITY_HIGH_ACCURACY位置。不過,我在清單中擁有此權限。 – Konrad

+0

您能否顯示您的權限的實施? –

+0

我剛剛把這個'LocationServices.FusedLocationApi.requestLocationUpdates( googleApiClient,locationRequest,this);'在我請求getLastLocation的同一個地方。現在沒有例外,但我仍然遇到與開始時相同的問題。我只改變我的onConnected方法,其餘的就像我的問題開始(我提供了完整的代碼) – Konrad

相關問題