使用android中的地理编码器类获取地址的空值

时间:2013-02-06 06:17:21

标签: android gps google-geocoder

我为地址获取空值并为其抛出异常,
我正在使用geocoder课程来获取地址
所以请帮帮我

我的代码是: -

String cityName = null;
String locType = null;
Geocoder gcd = new Geocoder(LocationActivity.this,
    Locale.getDefault());
List<Address> addresses;
try {

    addresses = gcd.getFromLocation(loc.getLatitude(),
    loc.getLongitude(), 1);
    Log.d("address", addresses.toString());
    if (addresses.size() > 0)
        System.out.println(addresses.get(0).getLocality());
    cityName = addresses.get(0).getLocality();
    locType = addresses.get(0).getAddressLine(0);
    String s = longitude + "\n" + latitude
                + "\n\nMy Current City name:" + cityName
                + "\n\nMy address:" + locType;

    editLocation.setText(s);

} catch (IOException e) {
    e.printStackTrace();
    String s = longitude + "\n" + latitude
            + "\n\nMy Current City name:" + cityName
            + "\n\nMy address:" + locType;

    editLocation.setText(s);

}

,例外是

02-06 11:09:50.641: W/GpsLocationProvider(59): Duplicate add listener for uid 10036
02-06 11:09:50.771: W/System.err(480): java.io.IOException: Service not Available
02-06 11:09:50.781: W/System.err(480):  at android.location.Geocoder.getFromLocation(Geocoder.java:117)
02-06 11:09:50.781: W/System.err(480):  at com.example.getcurrentlocation.LocationActivity$MyLocationListener.onLocationChanged(LocationActivity.java:149)
02-06 11:09:50.781: W/System.err(480):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
02-06 11:09:50.781: W/System.err(480):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
02-06 11:09:50.781: W/System.err(480):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
02-06 11:09:50.781: W/System.err(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 11:09:50.781: W/System.err(480):  at android.os.Looper.loop(Looper.java:123)
02-06 11:09:50.781: W/System.err(480):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 11:09:50.781: W/System.err(480):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 11:09:50.791: W/System.err(480):  at java.lang.reflect.Method.invoke(Method.java:521)
02-06 11:09:50.791: W/System.err(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 11:09:50.791: W/System.err(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 11:09:50.791: W/System.err(480):  at dalvik.system.NativeStart.main(Native Method)
02-06 11:10:29.471: W/System.err(480): java.io.IOException: Service not Available
02-06 11:10:29.471: W/System.err(480):  at android.location.Geocoder.getFromLocation(Geocoder.java:117)
02-06 11:10:29.471: W/System.err(480):  at com.example.getcurrentlocation.LocationActivity$MyLocationListener.onLocationChanged(LocationActivity.java:149)
02-06 11:10:29.481: W/System.err(480):  at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:191)
02-06 11:10:29.481: W/System.err(480):  at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:124)
02-06 11:10:29.481: W/System.err(480):  at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:140)
02-06 11:10:29.481: W/System.err(480):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-06 11:10:29.481: W/System.err(480):  at android.os.Looper.loop(Looper.java:123)
02-06 11:10:29.481: W/System.err(480):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-06 11:10:29.481: W/System.err(480):  at java.lang.reflect.Method.invokeNative(Native Method)
02-06 11:10:29.481: W/System.err(480):  at java.lang.reflect.Method.invoke(Method.java:521)
02-06 11:10:29.481: W/System.err(480):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-06 11:10:29.491: W/System.err(480):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-06 11:10:29.491: W/System.err(480):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

偶尔会发生这种情况。来自Google的Geocoder类存在问题。更好地使用https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding 您可以将cordinate值发送到此URL并检索位置。