我现在的位置总是返回null。我该如何解决这个问题? [英] My current location always returns null. How can I fix this?
问题描述
我想找到我的当前位置的机器人项目。当应用程序被加载我现在的位置总是空。我已经建立了在清单等权限,当我发现当前位置我打算用坐标找到距离到地图上的其他位置。我的code片段如下。为什么我总是得到一个空值?
locMan =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
标准暴击=新标准();
塔= locMan.getBestProvider(暴击,假);
位置= locMan.getLastKnownLocation(塔);
如果(位置!= NULL){
的System.out.println(位置不为空!);
土地增值税=(INT)(location.getLatitude()* 1E6);
隆基=(INT)(location.getLongitude()* 1E6);
的GeoPoint ourLocation =新的GeoPoint(LATI,隆基);
OverlayItem overlayItem =新OverlayItem(ourLocation,第一串,
第2个字符串);
CustomPinpoint定制=新CustomPinpoint(D,MainMap.this);
custom.insertPinpoint(overlayItem);
overlayList.add(自定义);
overlayList.clear();
} 其他 {
的System.out.println(定位为空!+塔);
Toast.makeText(MainMap.this,无法获得供应商,Toast.LENGTH_SHORT)
。显示();
}
getLastKnownLocation()
使用位置(S)$ P $其他应用程序pviously发现。如果没有应用程序已经做到了这一点,那么 getLastKnownLocation()
将返回null。
一件事,你可以做你的code有在得到的最后一个已知的位置 - 遍历所有已启用的提供者,而不仅仅是最佳供应商一个更好的机会。例如,
私人地点getLastKnownLocation(){
名单<字符串>供应商= mLocationManager.getProviders(真正的);
位置bestLocation = NULL;
对于(字符串提供商:提供商){
位置L = mLocationManager.getLastKnownLocation(供应商);
ALog.d(最后已知的位置,提供者:%S,位置:%s时,供应商,
升);
如果(L == NULL){
继续;
}
如果(bestLocation == NULL
|| l.getAccuracy()&其中; bestLocation.getAccuracy()){
ALog.d(找到最佳的最后已知位置:%s的,L);
bestLocation =升;
}
}
如果(bestLocation == NULL){
返回null;
}
返回bestLocation;
}
如果您的应用程序无法处理而无需一个位置,如果没有最后已知的位置,你需要监听位置更新。你可以看看这个类的一个实例,
<一个href="https://github.com/farble1670/autobright/blob/master/src/org/jtb/autobright/EventService.java">https://github.com/farble1670/autobright/blob/master/src/org/jtb/autobright/EventService.java
看方法 onStartCommand()
,它会检查,如果网络提供商已启用。如果不是,则使用最后的已知位置。如果已启用,它会注册接收位置更新。
I am trying to find my current location for an android project. When the application is loaded my current location is always null. I have set up the permissions in the manifest etc. When I find the current location I intend to use the coordinates to find distances to other locations on the map. My code snippet is below. Why do I always get a null value?
locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria crit = new Criteria();
towers = locMan.getBestProvider(crit, false);
location = locMan.getLastKnownLocation(towers);
if (location != null) {
System.out.println("Location is not null!");
lat = (int) (location.getLatitude() *1E6);
longi = (int) (location.getLongitude() * 1E6);
GeoPoint ourLocation = new GeoPoint(lati, longi);
OverlayItem overlayItem = new OverlayItem(ourLocation, "1st String",
"2nd String");
CustomPinpoint custom = new CustomPinpoint(d, MainMap.this);
custom.insertPinpoint(overlayItem);
overlayList.add(custom);
overlayList.clear();
} else {
System.out.println("Location is null! " + towers);
Toast.makeText(MainMap.this, "Couldn't get provider",Toast.LENGTH_SHORT)
.show();
}
getLastKnownLocation()
uses the location(s) previously found by other applications. if no application has done this, then getLastKnownLocation()
will return null.
one thing you can do to your code to have a better chance at getting as last known location- iterate over all of the enabled providers, not just the best provider. for example,
private Location getLastKnownLocation() {
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
ALog.d("last known location, provider: %s, location: %s", provider,
l);
if (l == null) {
continue;
}
if (bestLocation == null
|| l.getAccuracy() < bestLocation.getAccuracy()) {
ALog.d("found best last known location: %s", l);
bestLocation = l;
}
}
if (bestLocation == null) {
return null;
}
return bestLocation;
}
if your app can't deal without having a location, and if there's no last known location, you will need to listen for location updates. you can take a look at this class for an example,
https://github.com/farble1670/autobright/blob/master/src/org/jtb/autobright/EventService.java
see the method onStartCommand()
, where it checks if the network provider is enabled. if not, it uses last known location. if it is enabled, it registers to receive location updates.
这篇关于我现在的位置总是返回null。我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!