2017-09-06 17 views
1

私がしようとしているのは、ユーザーから位置を取得することです。まず、GPSを介して位置情報を取得しようとし、位置情報が利用可能かどうかをチェックします。そうでなければ、ネットワークタワーを通過しようとします。 GPSがオンになると正常に動作しています。私はオフにしたときにしかし、それはGPSがオンになっていなかったためセルタワーで位置を取得しようとしていますが、まだnullが返されます

manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    provider = manager.getBestProvider(new Criteria(), false); 

    Log.i("ITEMSET best", provider); 


    manager = (LocationManager) getSystemService(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) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 

    Log.i("ITEMSET provider", provider); 

    location = manager.getLastKnownLocation(provider); 
    Log.i("ITEMSET netLoc", location + ""); 
    if(location==null) { 
     provider = LocationManager.NETWORK_PROVIDER; 
     location = manager.getLastKnownLocation(provider); 
     Log.i("ITEMSET netLoc", location + "qqq"); 
    } 



    Log.i("ITEMSET current", provider); 
    Log.i("ITEMSET location", location + ""); 

Logcat

場所は、最初はnullたネットワークプロバイダを使用して場所を得ていないのGPS。しかし、プロバイダーをネットワークプロバイダーに変更しました。しかし、場所はまだヌル

+0

詳細な位置情報は許可されていますか? –

+0

はい私は素晴らしい場所と粗い場所の両方の権限を与えました。 –

+0

あなたが場所の更新を要求していない可能性があります。その理由は、その問題を与える理由です。 –

答えて

1

チェックするかどうかネットワークが有効になっているか、そしてない

isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 

:あなたはあなたの場所を得るためにGoogleApiClientを使用する必要があります

 if (isNetworkEnabled) { 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000,10, this); 
       Log.d("Network", "Network"); 
       if (locationManager != null) { 
        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 

         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 


        } 
       } 
+0

そうです。プロバイダのようなものが有効になっていないようです –

関連する問題