geoCoder 中不提供服务
我可以获得经度和纬度,但获取地址时出现错误。
public boolean onTouchEvent(MotionEvent event, 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;
}
我的 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): at com.parkhya.SecondGps.MainActivity$MapOverlay.onTouchEvent(MainActivity.java:47)
02-14 12:27:35.745: WARN/System.err(452): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
02-14 12:27:35.745: WARN/System.err(452): at com.google.android.maps.MapView.onTouchEvent(MapView.java:643)
02-14 12:27:35.757: WARN/System.err(452): at android.view.View.dispatchTouchEvent(View.java:3766)
02-14 12:27:35.757: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
02-14 12:27:35.757: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.767: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.767: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.777: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.777: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
02-14 12:27:35.777: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-14 12:27:35.777: WARN/System.err(452): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
02-14 12:27:35.786: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
02-14 12:27:35.786: WARN/System.err(452): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
02-14 12:27:35.796: WARN/System.err(452): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 12:27:35.796: WARN/System.err(452): at android.os.Looper.loop(Looper.java:123)
02-14 12:27:35.796: WARN/System.err(452): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 12:27:35.806: WARN/System.err(452): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 12:27:35.816: WARN/System.err(452): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 12:27:35.816: WARN/System.err(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 12:27:35.825: WARN/System.err(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 12:27:35.837: WARN/System.err(452): at dalvik.system.NativeStart.main(Native Method)
I am able to get longitude and latitude but getting address i got an error.
public boolean onTouchEvent(MotionEvent event, 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;
}
My 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): at com.parkhya.SecondGps.MainActivity$MapOverlay.onTouchEvent(MainActivity.java:47)
02-14 12:27:35.745: WARN/System.err(452): at com.google.android.maps.OverlayBundle.onTouchEvent(OverlayBundle.java:63)
02-14 12:27:35.745: WARN/System.err(452): at com.google.android.maps.MapView.onTouchEvent(MapView.java:643)
02-14 12:27:35.757: WARN/System.err(452): at android.view.View.dispatchTouchEvent(View.java:3766)
02-14 12:27:35.757: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:897)
02-14 12:27:35.757: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.767: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.767: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.777: WARN/System.err(452): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:863)
02-14 12:27:35.777: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
02-14 12:27:35.777: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
02-14 12:27:35.777: WARN/System.err(452): at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
02-14 12:27:35.786: WARN/System.err(452): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
02-14 12:27:35.786: WARN/System.err(452): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
02-14 12:27:35.796: WARN/System.err(452): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 12:27:35.796: WARN/System.err(452): at android.os.Looper.loop(Looper.java:123)
02-14 12:27:35.796: WARN/System.err(452): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 12:27:35.806: WARN/System.err(452): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 12:27:35.816: WARN/System.err(452): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 12:27:35.816: WARN/System.err(452): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 12:27:35.825: WARN/System.err(452): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 12:27:35.837: WARN/System.err(452): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是即使 Geocoder 失败也能获取地址的方法...但首先让我解释一下我自己使用 Geocoder 的经验:
出于某种未知的原因,Geocoder 工作正常,然后突然中断!。
我观察到,一段时间后(可能是几天),地理编码器开始抛出“服务不可用”异常。
编辑->正如 Christian 提到的,即使 isPresent() 返回 true,也会引发异常。<-编辑END< /strong>
真的很奇怪,因为它之前工作正常并且没有发生配置更改:
当然,有些东西发生了变化,但是,对于目前,我们还不知道到底是什么。
有一个关于此问题的错误:Issue 38009: 4.1.1 Geocoder throwing exception :IOException:服务不可用
此线程的最后一篇文章(ATTOW)“可能它会在 android 4.0.x + 目标 API x 中定期重现”
作为解决方法,您可以使用Google Map
这是我的GeocoderHelper类,它提供了“Google Map”B Plan,以防Geocoder启动失败。
就我而言,我只对 CityName 感兴趣,但您基本上可以在 google 地图 JSON 输出中获取任何可用数据
用法:
GeocoderHelper 类:
希望这会有所帮助。
Here is my way to get the address even if Geocoder failed... but first let me explain my own experience with Geocoder:
For some unknown reason, Geocoder works fine then, suddenly break!.
What I observed is that, after a while (can be days), Geocoder start to throw "Service not available" exception.
EDIT-> As mentioned by Christian, the exception is thrown even is isPresent() returns true.<-EDIT END
Really weird since it was working fine before and no configuration changed occurred:
Of course, something changed, but, for the time being, we don't know exactly what.
There is a bug opened about that: Issue 38009: 4.1.1 Geocoder throwing exception: IOException: Service not Available
Last post on this thread (ATTOW) "Probably it's periodically reproducible in android 4.0.x + target API x"
As a workaround, you can use Google Map
Here is my GeocoderHelper class that provides a 'Google Map' B Plan in case Geocoder start to fails.
In my case, I am just interested in CityName, but you can basically get any data availalbe in google map JSON output
Usage:
GeocoderHelper Class:
Hope this will help.
GeoCoder 服务可能会也可能不会被实现,因为它不是核心 Android 框架的一部分。 此处查看 GeoCoder 的文档
您需要使用 google API 并解析json 响应中的地址,使其适用于所有设备。
GeoCoder service may/maynot be implemented as it is not part of core android framework. Check the documentation for GeoCoder here
You need to use google APIs and parse the address from the json response for it to work on all the devices.
问题是 NetworkLocationService 可能不再运行。
只能通过重新启动来解决。 (NetworkLocationService 在启动时加载)
The problem is that NetworkLocationService may be no longer running.
It can be only solved by a reboot. (The NetworkLocationService is loaded at boot)