该应用程序使用Geocoder对象.它在我的股票Froyo Nexus One上运行良好.但后来我在不同的设备上运行完全相同的应用程序(Advent Vega 10"平板电脑也运行Froyo),我得到了这个例外:服务不可用.我正在使用的方法是getFromLocationName(),我正在建立对抗Android 1.6 Google API.
我知道在模拟器上抛出此异常的问题,但我怀疑这是不同的.为什么会在运行Froyo的设备上抛出它而不是另一台设备呢?
该应用程序是一个位置应用程序,由于平板电脑没有GPS或移动网络,在Wi-Fi连接不提供位置的情况下,用户必须手动指定它,因此无法使用Geocoder对象是坏消息.
我可以为用户添加一种在地图上选择位置的方法,但这并不理想.可能我可以直接使用谷歌地图API,但我希望首先了解问题的本质,因为有一个更简单的解决方案会很好.
希望在未来版本中,Android将包含非Geocoder设备的操作系统级"默认位置",因此位置感知应用程序可在Google TV等设备上开箱即用.
我能够得到经度和纬度,但得到地址我得到了一个错误.
public boolean onTouchEvent(MotionEvent事件,MapView mapView)
{
if(event.getAction()==0)
{
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault());
try
{
List<Address> address = geoCoder.getFromLocation( p.getLatitudeE6() / 1E6,
p.getLongitudeE6() / 1E6, 1);
String add="";
if(address.size()>0)
{
for (int i=0; i<address.get(0).getMaxAddressLineIndex();i++)
add += address.get(0).getAddressLine(i) + "\n";
}
Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
} catch (IOException e)
{
e.printStackTrace();
}
return true;
}
return false;
Run Code Online (Sandbox Code Playgroud)
}
我的Logcat - >
02-14 12:27:35.717: WARN/System.err(452): java.io.IOException: Service not Available
02-14 12:27:35.727: WARN/System.err(452): at android.location.Geocoder.getFromLocation(Geocoder.java:117)
02-14 12:27:35.727: WARN/System.err(452): …Run Code Online (Sandbox Code Playgroud)