- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我刚刚开始工作,但是当我关闭 Wifi 以查看是否可以使用 GPS 获得更准确的位置时,它现在给我带来 NullPointer
错误,我看不到为什么。
下面的代码首先被调用;
public void onConnected(Bundle dataBundle) {
connected = true;
//Request an update from the location client to get current Location details
mLocationClient.requestLocationUpdates(mLocationRequest,this);
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
然后需要调用此方法
public void onLocationChanged(android.location.Location location) {
// Report to the UI that the location was updated
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
if(tmp != location.getLatitude())
{
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
tmp = location.getLatitude();
}
您可以看到我在那里有 2 个 toast ,它们返回的实际 GPS 坐标很好,但是当我调用下面的代码时,它会在新的位置行 getLastKnownLocation
public String getLocation()
{
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat,lon;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try {
lat = location.getLatitude ();
lon = location.getLongitude ();
Toast.makeText(this,""+lat.toString()+"-"+lon.toString(),Toast.LENGTH_SHORT).show();
return new LatLng(lat, lon).toString();
}
catch (NullPointerException e){
Toast.makeText(this,"HELL-NO",Toast.LENGTH_SHORT).show();
Log.e("HELL-NO","n",e);
e.printStackTrace();
return null;
}
}
我只是不明白为什么当我尝试检索似乎已在 onLocationChanged
方法中检索到的位置时,它只是提供了一个空指针。
最佳答案
我只是改变了
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) to
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
这为我解决了解决方案。完整 fragment :
map = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
getLastLocation = locationManager.getLastKnownLocation
(LocationManager.PASSIVE_PROVIDER);
currentLongitude = getLastLocation.getLongitude();
currentLatitude = getLastLocation.getLatitude();
currentLocation = new LatLng(currentLatitude, currentLongitude);
希望这会有所帮助。
关于android - getLastKnownLocation 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19621882/
我正在使用 Google map v2,有时 getLastKnownLocation() 返回 NULL。有没有其他方法可以达到相同的效果但稳定性更高?感谢您的帮助 最佳答案 首先,如果用户关闭了您
我想每 5 分钟从我的 Android 应用向服务器发送一次最新和最准确的位置。 在此question我被建议创建一个将注册到 LocationUpdates 的 alarmManager。 为什么这
我正在尝试检索设备的位置,但由于某种原因没有接收到该位置。有人可以检查我的代码并让我知道代码中的错误是什么吗?谢谢。 主 Activity public class MainActivity ext
来自 android 文档 LocationManager.getLastKnownLocation(): Returns a Location indicating the data from th
我正在尝试通过最佳提供商从 gps/netwrok 获取位置,但它总是返回相同的位置,而且我可以在谷歌地图上看到我正确位置的标志。谁能告诉我我做错了什么? 我的 Activity 是: public
在我的应用程序中,我有一个 AlarmManager 设置为每隔一段时间运行一个 IntentService 以向我的服务器发送您的当前位置的 WebService 调用 启动警报后,我还会启动一个
所以我正在使用 getLastKnownLocation 方法获取设备的位置位置,但除非有 GPS 定位,否则它会返回 null。有没有办法延迟 getLastKnownLocation 的执行,直到
我通过执行来设置我的位置管理器 LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 然
我正在开发处理 Google Maps Api 的 Android 应用程序,我尝试使用 mMap.getLastKnownLocation(); 获取我的当前位置,然后获取 latitude 和 l
固定位置有问题。我使用 getLastKnownLocation(LocationManager.NETWORK_PROVIDER);但总是返回 null,我已经在你的 AndroidManifest
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我正在开发一个 Android 应用程序,其中当前和精确的位置是关键。检测到 GPS 定位后,它会转到下一个 Activity 。 问题是我在户外测试它,发现有时它只是使用以前修复的位置。 所以我问的
getBestKnownLocation Returns a Location indicating the data from the last known location fix obtaine
我正在使用 myLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 在我的应用程序启动时检索
这似乎是一个普遍的问题,但我真的不明白为什么会发生这种情况,即使我阅读了大量类似的问题。 我正在模拟器设备上玩一个基本的位置类,我设置了所有内容 - 权限(FINE 和 COARSE),我在 DDMS
我正在为 GPS 提供商调用 getLastKnownLocation,它返回空值。文档说如果未启用提供程序可能会发生这种情况,但我知道它是。如果最后一个已知位置不存在,提供者是否可以返回 null?
我刚刚开始工作,但是当我关闭 Wifi 以查看是否可以使用 GPS 获得更准确的位置时,它现在给我带来 NullPointer 错误,我看不到为什么。 下面的代码首先被调用; public void
我已经阅读了一些关于此的问题,但我并没有找到我需要的答案。 所以,情况是我设置了我的 map ,我想获取我当前的 gps 位置。我检查了我的变量不是 NULL,但我的结果来自: getLastKnow
我正在创建一个仅在用户单击按钮时检查位置的应用程序。 这是我的代码,当我调用 getLastKnownLocation 时,即使在调用 requestLocationUpdates 之后,它也会返回
我尝试通过 GPS 获取当前位置并显示它。 首先,我初始化 LocationManager,因此我总是使用方法 getLocation() public class MainActivity exte
我是一名优秀的程序员,十分优秀!