gpt4 book ai didi

java - 我当前位置附近的城市或城镇

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:02 26 4
gpt4 key购买 nike

如何从我在 android 中的当前位置找到附近的城市或城镇。我可以找到最近的地方,例如 ATM、医院。但是,我无法获得最近的城镇或城市名称。

最佳答案

这是一个例子:

https://github.com/yangjiandong/proAndroid4DevCode/blob/master/Full%20Worked%20Sample%20Projects/Chapter_13_Where_Am_I_Part_3/src/com/paad/whereami/WhereAmI.java#L14

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

String latLongString = "No location found";
String addressString = "No address found";

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

double latitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this, Locale.getDefault());

try {
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0) {
Address address = addresses.get(0);

for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");

sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
} catch (IOException e) {}
}

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

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}

关于java - 我当前位置附近的城市或城镇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35746944/

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