2016-07-16 2 views
-1

タイトルが言うように、私はAndroidの開発者サイトにあるコードを守り、現在地の緯度と経度を取得しようとしています。私はAndroid Studioとエミュレータを使用しています。 getLatitude()とgetLongitude()を使用しているときに私のコードがnullを渡す理由がわかりません - 指定されたコードに従ったと思いました。Android - LocationServicesが現在の場所を取得しようとしたときにnullを渡しています

ありがとうございます!

(クラスで必要なもの)HomePage.java:

public void onConnected(@Nullable Bundle bundle) { 
    if (location == null){ 
     declareLocation(); 
    } 
    double latitude = location.getLatitude(); 
    double longitude = location.getLongitude(); 
    SharedPreferences sharedPreferences = getSharedPreferences("example.com.musicapptest", Context.MODE_PRIVATE); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putLong("latitude", Double.doubleToLongBits(latitude)); 
    editor.putLong("longitude", Double.doubleToLongBits(longitude)); 
} 
private Location declareLocation(){ 
    try{ 
     location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    } 
    catch (SecurityException e){ 
     e.printStackTrace(); 
    } 
    return location; 
} 
+0

可能な複製(http://stackoverflow.com/questions/16830047/locationclient-getlastlocation-return-null ) –

答えて

0

多くの要因がこれを引き起こしている可能性があります。 ApiClientは接続されていますか?正しく構築しましたか?これらの両方が当てはまる場合、場所は利用できません。参照引用

は、現在利用可能な最良の最新の位置を返します。

非常にまれに起こるはずの場所がない場合、nullが返されます。場所の権限を尊重しながら利用可能な最高の精度が返されます。

この方法は、場所を簡単に取得する方法を提供します。これは、正確な場所を必要とせず、場所の更新に余分なロジックを維持したくないアプリケーションに特に適しています。

getLocationAvailability().isLocationAvailable()を使用して、nullが返されるかどうかを調べることもできます。このメソッドがfalseを返す場合は、requestLocationUpdates()メソッドを使用して位置の更新を要求する必要があります。あなたは、このガイドを参照することができます位置情報の更新について

、:[LocationClient getLastLocation()の戻りヌル]のhttps://developer.android.com/training/location/index.html

関連する問題