如何获得设备的当前纬度和经度?

时间:2019-03-31 21:15:15

标签: java android android-location

我正在尝试返回当前位置作为纬度和经度。我尝试了以下代码,但是在调用getLastKnownLocation()时出现onClick错误。

public void displayCurrentLocation(View view){

        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();
        Toast.makeText(getApplicationContext(),"Latitude:" + latitude + " Longitude:" + longitude,Toast.LENGTH_SHORT);

    }

还有另一种方法吗?我尝试了使用fusedLocationClient的另一种方法,但不确定如何正确设置它。

0 个答案:

没有答案
相关问题