gpt4 book ai didi

android - 在Android中查找当前位置的经纬度

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:50:39 25 4
gpt4 key购买 nike

<分区>

我想在 android 应用程序中找到我当前位置的纬度和经度。当我的手机稳定时,我才需要它。

我的代码是:

LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);


locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}

private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);

String latLongString;

if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}

myLocationText.setText("Your Current Position is:\n" +
latLongString);
}

在模拟器上运行后,我总是发现“找不到位置”。为什么会这样?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com