Android - 地理编码器未获取地址字符串

Android - Geocoder not getting address string

我有以下代码在提供经纬度后获取完整的地址字符串。但它一直在给出例外。我不知道为什么这段代码没有返回地址。请帮忙。谢谢

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!

地理编码器非常不稳定。当它进入您报告的状态时,通常会 "fixes" 重新启动设备。如果您想要一个可靠的解决方案,我建议您查看 Google 地理编码 API.