4

我写了一个简单的 Activity 来测试 Geocoder 的存在,调用 Geocoder.isPresent() 总是返回 false。

班级:

public class LocationTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LocationManager locationManager = (LocationManager)  this.getSystemService(Context.LOCATION_SERVICE);
        Log.d( "LocationTestActivity", "Geocoder.isPresent : " + Geocoder.isPresent() );

    }
}


AndroidManifest.xml 在“application”元素之前还有以下条目:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


环境:Eclipse Indigo 3.7.1,XP Professional 2002 SP 3 上的 ICS 4.0 模拟器

请帮助我理解:
1. 为什么 Geocoder.isPresent() 总是返回错误?
2. 为了使 Geocoder.isPresent() 返回 true,需要进行哪些更改?

提前非常感谢!

4

3 回答 3

5

实际上Geocoder需要一个由框架在后台运行的服务。

从文档中:

如果平台中没有后端服务,Geocoder 查询方法将返回一个空列表。使用 isPresent() 方法确定是否存在 Geocoder 实现。

因此,如果我们查看 的文档isPresent(),它会说明。

如果实现了地理编码器方法 getFromLocation 和 getFromLocationName,则返回 true。缺乏网络连接仍可能导致这些方法返回 null 或空列表。

注意:请记住,这isPresent()在 Pre-Api 9 平台中不可用。

于 2012-01-31T06:49:53.053 回答
2

用于使用.AsyncTask从服务器获取坐标geocoder。例如,getFromLocationName()应该使用AsyncTask. UI 线程(主要活动)不允许花费太多时间的任务,因此该方法返回空列表。

于 2016-06-18T12:25:28.620 回答
1

在模拟器或设备中测试此代码?我在 2.2 模拟器上使用 GeoCoder 时遇到了同样的问题。但是代码在 2.1 模拟器上运行良好。尝试使用 2.1

并且代码必须在设备上运行良好。

于 2012-01-31T06:43:55.740 回答