地理编码android - 根据坐标查找地址
我在我的 Android 应用程序中组合了这个小函数:
protected void toAddress(double lat, double lng, int maxResults) {
String address = null;
Geocoder gcd = new Geocoder(TarsActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(lat, lng, maxResults);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
address = addresses.get(0).getLocality().toString();
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG );
}
else
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG);
}
函数调用如下所示
toAddress(location.getLatitude(), location.getLongitude(), 10);
但应用程序不显示任何带有地址的 Toast?我已经对其进行了调试,正确的地址位于地址列表中。正如您所看到的,我尝试使用 toString() 函数将其转换为字符串。有人吗?提前致谢!
I've put together this little function in my android app:
protected void toAddress(double lat, double lng, int maxResults) {
String address = null;
Geocoder gcd = new Geocoder(TarsActivity.this, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = gcd.getFromLocation(lat, lng, maxResults);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
address = addresses.get(0).getLocality().toString();
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG );
}
else
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG);
}
The function call goes like this
toAddress(location.getLatitude(), location.getLongitude(), 10);
But the app doesn't display any Toast with the address? I've debugged it and the correct address is in the addresses-list. As you can see I've tried to convert it to a string with the toString()-function. Anybody? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试:
Toast.makeText(getApplicationContext(), 地址, Toast.LENGTH_LONG ).show();
创建 Toast 后,您不会调用 .show()。
Try:
Toast.makeText(getApplicationContext(), address, Toast.LENGTH_LONG ).show();
You are not calling .show() after you create the Toast.