为什么LocationManager不具有lastKnown位置? [英] Why does the LocationManager does not have a lastKnown location?

查看:118
本文介绍了为什么LocationManager不具有lastKnown位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用户的位置,而且也只有一次之后,该用户导航在自己的

I want the location of user and that too just once after that user navigates on his own

locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
if (location != null) {
   System.out.println(location.toString());
   lat=location.getLatitude();
   lng=location.getLongitude();
}     
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));

但是我不能够获取用户的位置,是因为从移动??

However i am not able to get the location of the user is it because of slow internet connection from the mobile ??

推荐答案

这是不够的,只是得到一个locationManager并调用 getLastKnownLocation() 因为直到有更新就没有最后已知位置 你缺少的LocationManager类提供的这一要求:

It's not enough to just get a locationManager and call getLastKnownLocation() because until there is an update there will be no "last known location" You are missing that request provided by the LocationManager class:

public void requestLocationUpdates(java.lang.String provider, 
                                   long minTime,
                                   float minDistance, 
                                   android.location.LocationListener listener) 

如果你的活动实现LocationListener的矿山一样,你可以通过在本的LocationListener的一个呼叫始发的那个类的方法。否则,有服用活动或尺蠖类,而不是其他一些重载。

If your activity implements LocationListener as mine did, you can pass in "this" as the LocationListener for a call originating in a method of that class. Otherwise, there are several other overloads taking Activity or Looper classes instead.

locationManager.requestLocationUpdates(provider, minTime, minDistance, this);

为了确保该设备得到一个位置,要求更新 - 但你也可以 希望把警卫未能找到提供者或 getLastKnownLocation 返回null。

In order to make sure the device is getting a location, request updates - but you also may want to put guards for failure to find providers or getLastKnownLocation returning null.

locationManager.requestLocationUpdates(provider, 1, 0, this);
mostRecentLocation = locationManager.getLastKnownLocation(provider);
if(mostRecentLocation == null)
// lower your expectations, or send user message with Toast or something similar 

告诉用户打开其位置共享或其他供应商的服务。

Tell the user to turn on their location sharing or other provider services.

这篇关于为什么LocationManager不具有lastKnown位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆