gpt4 book ai didi

java - 如何在 Android 上使用 GPS 输出位置

转载 作者:行者123 更新时间:2023-12-02 07:03:26 25 4
gpt4 key购买 nike

我正在尝试将位置输出到 TextView 中。我已经使用了 locationManager.toString(),但它只会给我输出 android.location.LocationManager@44ee7718。我想输出位置,例如 Los Angeles, CA 90501。

LocationManager 的代码:

LocationManager lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//conversion
Criteria criteria = new Criteria();
String bp = lm.getBestProvider(criteria, false);
Location location = lm.getLastKnownLocation(bp);
double lat = location.getLatitude();
double lon = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
try{
addresses = gc.getFromLocation(lat, lon, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Address address = addresses.get(0);
final String ad = address.getAddressLine(0);
//ends here

按钮的代码:

Red.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.putExtra("sms_body", ad);
intent.setType("vnd.android-dir/mms-sms");
startActivity(intent);
}
});

最佳答案

尝试以下代码

try {
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager
.getLastKnownLocation(bestProvider);
double lat = location.getLatitude();
double lon = location.getLongitude();
Geocoder gc = new Geocoder(this);
List<Address> lstAdd = gc.getFromLocation(lat, lon, 1);
Address ad = lstAdd.get(0);
String str = ad.getAddressLine(0);
Toast tst = Toast.makeText(this, "Your current location is " + str,
Toast.LENGTH_LONG);
tst.show();
} catch (Exception e) {
Toast tst = Toast.makeText(this,"Please check your gps settings",
Toast.LENGTH_LONG);
tst.show();
}

希望这有帮助。

关于java - 如何在 Android 上使用 GPS 输出位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16371840/

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