![](/img/trans.png)
[英]How to get current Location by GPS or WiFi when MapActivity starts
[英]get the current location (gps/wifi)
我正在嘗試使用它來獲取我的位置:
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
傳遞給requestLocationUpdates()
方法 ,這將為您提供異步更新你所在的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.