当我在不同的设备上运行时,为什么使用Geocoder返回的地址不同?

时间:2015-03-24 12:43:14

标签: android google-geocoder

我在两个不同的设备中测试了以下代码,一个是4.3,另一个是4.1.2.4.3设备给我的地址,而我使用的平板电脑(4.1.2)在getFromLocation()中给出了异常.I不明白为什么两个设备提供两种不同的输出。

Geocoder geocoder = new Geocoder(AAAView.this, Locale.getDefault());
            // Get the current location from the input parameter list
            Location loc = params[0];
            // Create a list to contain the result address
            List<Address> addresses ;
            try {

                lat=loc.getLatitude();
                 lon=loc.getLongitude();

         addresses = geocoder.getFromLocation(lat,lon, 1);

           System.out.println("The address is " + addresses );

            } catch (IOException e1) {
            Log.e("LocationSampleActivity",
                    "IO Exception in getFromLocation() ");
            e1.printStackTrace();
            return ("Cannot get address");
            }

Logcat错误

03-24 18:51:54.501: W/System.err(12673): java.io.IOException: Service not Available
03-24 18:51:54.509: W/System.err(12673):    at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-24 18:51:54.509: W/System.err(12673):    at com.assuretech.ku.aaa.AAAView$GetAddressTask.doInBackground(AAAView.java:1070)
03-24 18:51:54.517: W/System.err(12673):    at com.assuretech.ku.aaa.AAAView$GetAddressTask.doInBackground(AAAView.java:1)
03-24 18:51:54.517: W/System.err(12673):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-24 18:51:54.525: W/System.err(12673):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-24 18:51:54.525: W/System.err(12673):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-24 18:51:54.525: W/System.err(12673):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
03-24 18:51:54.533: W/System.err(12673):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-24 18:51:54.533: W/System.err(12673):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-24 18:51:54.541: W/System.err(12673):    at java.lang.Thread.run(Thread.java:856)

1 个答案:

答案 0 :(得分:2)

地理编码api取决于操作系统服务,在您执行查询时需要运行该服务。因此,没有服务的设备将抛出异常。

请参阅文档:Geocoder

此外,您可以使用isPresent()方法确定特定设备的服务是否可用。

在这里查看我的答案:Geocoder's isPresent() method always returns false

为了让它正常工作,我建议使用Geocoding api

相关问题