获取空从SDK“getLastKnownLocation” [英] Getting null from 'getLastKnownLocation' on SDK

查看:131
本文介绍了获取空从SDK“getLastKnownLocation”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相关的位置API的一个问题。

I have a problem related to the Location API.

我尝试以下code:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);

位置总是,当 getLastKnownLocation()之称。

什么是错的?

推荐答案

随着你的的Andr​​oidManifest.xml 文件的权限,有你注册一个位置监听器?

Along with the permissions in your AndroidManifest.xml file, have you registered a location listener?

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location loc = getLastKnownLocation(LocationManager.GPS_PROVIDER);
lm.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);

然后有一个方法,在这种情况下, LocationListener的,完成你的任务。

private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

这篇关于获取空从SDK“getLastKnownLocation”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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