2017-10-12 3 views
0

関数getLastKnownLocation()を使ってLast Known Locationを取得しようとすると、緯度と経度を取得してtextFieldに設定しますが、locationという名前のlocationのインスタンスはnullを返すため、緯度と経度を取得できず、textFieldの位置は使用できません最後に知られている場所を得る?どのように関数getLastKnownLocation()を使用しますか?

   public class ShowLocationActivity extends Activity implements LocationListener { 
       private TextView latituteField; 
       private TextView longitudeField; 
       private LocationManager locationManager; 
       private String provider; 

       /** Called when the activity is first created. */ 
       @Override 
       public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_show_location); 
        latituteField = (TextView) findViewById(R.id.TextView02); 
        longitudeField = (TextView) findViewById(R.id.TextView04); 

        // Get the location manager 
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
        // Define the criteria how to select the locatioin provider -> use 
        // default 
        Criteria criteria = new Criteria(); 
        provider = locationManager.getBestProvider(criteria, false); 
        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) 
        { 
         Location location = locationManager.getLastKnownLocation(provider); 

         // Initialize the location fields 
         if (location != null) { 
          System.out.println("Provider4 " + provider + " has been selected."); 
          onLocationChanged(location); 
         } else { 
          latituteField.setText("Location not available"); 
          longitudeField.setText("Location not available"); 
         } 
        } 
       } 

       /* Request updates at startup */ 
       @Override 
       protected void onResume() { 
        super.onResume(); 
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) 
        locationManager.requestLocationUpdates(provider, 400, 1, this); 
       } 

       /* Remove the locationlistener updates when Activity is paused */ 
       @Override 
       protected void onPause() { 
        super.onPause(); 
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
          == PackageManager.PERMISSION_GRANTED) 
        locationManager.removeUpdates(this); 
       } 

       @Override 
       public void onLocationChanged(Location location) { 
        int lat = (int) (location.getLatitude()); 
        int lng = (int) (location.getLongitude()); 
        latituteField.setText(String.valueOf(lat)); 
        longitudeField.setText(String.valueOf(lng)); 
       } 

       @Override 
       public void onStatusChanged(String provider, int status, Bundle extras) { 
        // TODO Auto-generated method stub 

       } 

       @Override 
       public void onProviderEnabled(String provider) { 
        Toast.makeText(this, "Enabled new provider " + provider, 
          Toast.LENGTH_SHORT).show(); 

       } 

       @Override 
       public void onProviderDisabled(String provider) { 
        Toast.makeText(this, "Disabled provider " + provider, 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

}

答えて

0

使用このコードsnipetを最後の既知の位置を取得する:

依存関係を追加:

implementation 'com.google.android.gms:play-services-location:11.4.0' 

と権限を追加します。

をその後

@SuppressWarnings("MissingPermission") 
    private void getLastLocation() { 
    FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); 

     mFusedLocationClient.getLastLocation() 
       .addOnCompleteListener(new OnCompleteListener<Location>() { 
        @Override 
        public void onComplete(@NonNull Task<Location> task) { 
         if (task.isSuccessful() && task.getResult() != null) { 
          mLastLocation = task.getResult(); 
          double lat = mLastLocation.getLatitude(); 
          double log = mLastLocation.getLongitude(); 
          Log.d(TAG, "lat:"+lat+"::"+log); 

         } else { 
          Timber.w("getLastLocation:exception::" + task.getException()); 

         } 
        } 
       }); 
    } 

Getting the Last Known Location

+0

は私が許可 コンパイル「com.google.android.gmsを追加するものの、 "FusedLocationProviderClientを解決できない" FusedLocationProviderClientに誤りがあり、より詳細後藤は:-サービスを演じます:9.2.0 ' ' com.google.android.gms:play-services-location:9.2.0 'をコンパイルします。 どうしますか? –

+0

あなたの依存関係を更新します。 – vinay

関連する問題