簡體   English   中英

locationManager.getLastKnownLocation請求后返回null

[英]locationManager.getLastKnownLocation returns null after request

我嘗試通過GPS獲取當前位置並顯示它。

首先,我初始化LocationManager,所以我總是要使用getLocation()方法

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener, LocationListener {

    private static final int REQUEST_LOCATION = 1;
    private static int MY_PERMISSIONS_REQUEST_ACCESS;

    TextView locationText;

    LocationManager locationManager;
    String lattitude,longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Button buttonLocate = (Button) findViewById(R.id.buttonLocate);

        locationText = (TextView)findViewById(R.id.locationText);

        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

        buttonLocate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(locationManager != null) {
                    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        // ..DO SMTH
                    } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                        getLocation();
                    }

                }
            }
        });

    }

在方法getLocation()我正在檢查權限,如果被授予,我將請求位置更新,並嘗試獲取緯度和經度,這是我的問題,因為位置始終為null ,所以我無法獲取坐標! 代碼有什么問題,我該如何解決?

void getLocation() {

    if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
            (MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

    } else {

    // REQUEST!

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);

        Location location;


        if (locationManager != null) {
               location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                 if (location != null) {
                        // LOCATION IS ALWAYS NULL!!!

                double latti = location.getLatitude();
                double longi = location.getLongitude();
                lattitude = String.valueOf(latti);
                longitude = String.valueOf(longi);

                locationText.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
                        + "\n" + "Longitude = " + longitude);

            } else {

                Toast.makeText(this, "Unble to Trace your location", Toast.LENGTH_SHORT).show();

            }
        } 
    }

getLastKnownLocation返回設備的最后一個已知位置。 如果沒有最后一個已知的位置緩存,它將返回null。

getLastKnownLocation為null時,您需要請求位置更新。

為了獲得位置更新,您必須實現onLocationChanged

public void onLocationChanged(Location location) {
    // Called when a new location is found by the network location provider.
    Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM