我已经通过这个堆栈溢出和其他具有反向地理编码的相关文章浏览了几篇文章。
我打算从触摸事件中找到具有给定纬度和经度的地方的名称。我从触摸事件中获得了纬度和经度,但我无法从该地理坐标中获得地址,它通过异常而不是在尝试区域中,
这是代码:
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6() / 1E6, p.getLongitudeE6() / 1E6, 1);
String strCompleteAddress= "";
if (addresses.size() > 0)
{
for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
}
Log.i("MyLocTAG => ", strCompleteAddress);
Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
}
catch (IOException e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "exception", Toast.LENGTH_LONG).show();
}
return true;
}
else
return false;
}
}
请让我找到解决方案。