Android - Geocoder没有获取地址字符串

时间:2015-04-05 03:56:22

标签: java android geocoding reverse-geocoding google-geocoding-api

在提供lat和lng之后,我有以下代码来获取完整的地址字符串。但它一直都是例外。我不知道为什么这段代码没有返回地址。请帮忙。感谢。

private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
        String strAdd = "";
        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
            if (addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder("");

                for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
                }
                strAdd = strReturnedAddress.toString();
                Log.w("My Current loction address", "" + strReturnedAddress.toString());
            } else {
                Log.w("My Current loction address", "No Address returned!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.w("My Current loction address", "Canont get Address!");
        }
        return strAdd;
    }

logcat的

04-05 09:28:44.217: I/SurfaceTextureClient(4578): [STC::queueBuffer] this:0x55815618, api:1, last queue time elapsed :225623 ms
04-05 09:28:44.218: I/SurfaceTextureClient(4578): [0x55815618] frames:2, duration:225.699997, fps:0.008861
04-05 09:28:44.265: V/Provider/Setting(4578): from settings cache , name = sound_effects_enabled value = 0
04-05 09:28:44.290: W/System.err(4578): java.io.IOException: Service not Available
04-05 09:28:44.342: W/System.err(4578):     at android.location.Geocoder.getFromLocation(Geocoder.java:139)
04-05 09:28:44.342: W/System.err(4578):     at com.beproject.ourway.Panic.getCompleteAddressString(Panic.java:70)
04-05 09:28:44.343: W/System.err(4578):     at com.beproject.ourway.Panic.getLocation(Panic.java:102)
04-05 09:28:44.343: W/System.err(4578):     at com.beproject.ourway.Panic.onOptionsItemSelected(Panic.java:158)
04-05 09:28:44.343: W/System.err(4578):     at android.app.Activity.onMenuItemSelected(Activity.java:2546)
04-05 09:28:44.343: W/System.err(4578):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:971)
04-05 09:28:44.343: W/System.err(4578):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
04-05 09:28:44.344: W/System.err(4578):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
04-05 09:28:44.344: W/System.err(4578):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
04-05 09:28:44.344: W/System.err(4578):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:523)
04-05 09:28:44.344: W/System.err(4578):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:100)
04-05 09:28:44.344: W/System.err(4578):     at android.view.View.performClick(View.java:4091)
04-05 09:28:44.345: W/System.err(4578):     at android.view.View$PerformClick.run(View.java:17072)
04-05 09:28:44.345: W/System.err(4578):     at android.os.Handler.handleCallback(Handler.java:615)
04-05 09:28:44.345: W/System.err(4578):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 09:28:44.346: W/System.err(4578):     at android.os.Looper.loop(Looper.java:153)
04-05 09:28:44.346: W/System.err(4578):     at android.app.ActivityThread.main(ActivityThread.java:4987)
04-05 09:28:44.346: W/System.err(4578):     at java.lang.reflect.Method.invokeNative(Native Method)
04-05 09:28:44.346: W/System.err(4578):     at java.lang.reflect.Method.invoke(Method.java:511)
04-05 09:28:44.346: W/System.err(4578):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-05 09:28:44.346: W/System.err(4578):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-05 09:28:44.347: W/System.err(4578):     at dalvik.system.NativeStart.main(Native Method)
04-05 09:28:44.347: W/My Current loction address(4578): Canont get Address!

1 个答案:

答案 0 :(得分:1)

Geocoder很不稳定。当它进入你报告的状态时,重启设备通常会“修复”它。如果您需要可靠的解决方案,我建议您查看Google地理编码API。