我正在尝试使用它来获取我的位置,如下所示:
LocationManager myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = myLocationManager.getBestProvider(criteria,true);
if (provider != null) {
//there is a provider
myLocationManager.requestLocationUpdates(provider, 10L, 500.0f, (LocationListener) mainContext);
Location myCurLocation = myLocationManager.getLastKnownLocation(provider);
//here i'm trying to get some data from the
//myCurLocation but
//myCurLocation == NULL
}
但 myCurLocation 始终 == NULL
我究竟做错了什么?
致电给getLastKnownLocation()
不阻塞 - 这意味着它将返回null
如果当前没有空缺职位 - 那么您可能想看看通过LocationListener http://developer.android.com/intl/de/reference/android/location/LocationListener.html to the requestLocationUpdates() method http://developer.android.com/intl/de/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.app.PendingIntent)相反,这将为您提供位置的异步更新。
看一下这个问题是一个使用的例子LocationListener https://stackoverflow.com/questions/2227292/how-to-get-latitude-and-longitude-of-the-mobiledevice-in-android.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)