获取位置android? [英] Getting location android?
问题描述
我想在一小段时间内每秒接收一次位置更新,所以我写了这段代码:
I want to receive location updates every second for a small period of time, so I wrote this code:
public void registerListener() {
if (ActivityCompat.checkSelfPermission(c, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(c, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
} else {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, locationListener);
}
}
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
Log.v(TAG, ""+longitude + " " +latitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
我从其他地方拨打 registerListener()
。问题是,我的位置更新只调用一次,我只获得一次该日志。此外,在调用 registerListener
之后,日志只有5-10秒。
I call registerListener()
from somewhere else. The problem is, my location update is only called once, and I only get that log once. Also, the log only comes about 5-10 seconds after calling registerListener
.
谢谢,
Ruchir
推荐答案
您正在申请GPS位置。第一次获得卫星锁定需要几秒钟。如果您无法接收到信号锁(在室内某些建筑物,地下太远,手机对物理上的损坏,如松散的天线(在早期的三星公司中常见)),可能永远不会发生。)
You're requesting GPS location. It will take a few seconds to get a satellite lock for the first time. It may never happen if you can't receive a signal lock (indoors in some buildings, too far underground, physical damage to the phone like a loose antenna (common in early gen Samsungs)).
至于再没有收到它 - 除非你移动至少10米,否则你不会再收到它。将其更改为0,每秒看一次。
As for not receiving it again- you won't receive it again unless you move at least 10 meters. Change that to 0 and see it come every second.
这篇关于获取位置android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!