Geocoder 的 isPresent() 方法始终返回 false
我编写了一个简单的 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() 总是返回 false?
2. 需要进行哪些更改才能使 Geocoder.isPresent() 返回 true?
提前非常感谢!
I had written a simple Activity to test presence of Geocoder, calling Geocoder.isPresent() always returns false.
Class:
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 ALSO has following entries before "application" element:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Environment : Eclipse Indigo 3.7.1, ICS 4.0 emulator on XP Professional 2002 SP 3
Please help me understand:
1. Why Geocoder.isPresent() is always returing false?
2. What changes to make so that Geocoder.isPresent() returns true?
Thanks much in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上
Geocoder
需要框架在后台运行的服务。从文档中:
因此,如果我们查看
isPresent()
的文档,它指出。注意:请记住,
isPresent()
在 Pre-Api 9 平台中不可用。Actually the
Geocoder
need a Service running in the background by the framework.From the documentation:
so if we look at the documentation of
isPresent()
, it states.Note: keep in mind that
isPresent()
is not available in Pre-Api 9 plateforms.使用
AsyncTask
使用geocoder
从服务器获取坐标。例如,应使用AsyncTask
调用getFromLocationName()
。 UI线程(主活动)不允许执行花费太多时间的任务,因此该方法返回空列表。Use
AsyncTask
to fetch coordinates from server usinggeocoder
. For example,getFromLocationName()
should be called usingAsyncTask
. UI thread (main activity) does not allow the tasks which take too much time, hence the method returns empty list.在模拟器或设备中测试此代码?当我在 2.2 模拟器上使用 GeoCoder 时,我遇到了同样的问题。但代码在 2.1 模拟器上运行良好。尝试使用 2.1
并且代码必须在设备上正常运行。
Testing this code in Emulator or device ? I have faced same problem when I was using GeoCoder on 2.2 emulator. But code works fine on 2.1 emulator. Try to use 2.1
And code must be running fine on device.