我使用此代码查找我的位置(纬度和经度),但此代码有时为 快速响应(几秒钟),有时延迟超过10分钟。 问题在哪里?
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10L,
5.0f, locationListener);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView) findViewById(R.id.text);
String latLongString = "";
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" + latLongString);
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
}
答案 0 :(得分:1)
您正在使用GPS_PROVIDER获取位置。
GPS->此提供商使用卫星确定位置。根据具体情况,此提供商可能需要一段时间才能返回位置修复,并提供大约20英尺的准确结果。
您可以使用的另一个提供商是NETWORK_PROVIDER。 该提供商根据手机信号塔和WiFi接入点的可用性确定位置,并提供大约200英尺的准确结果,并且它比GPS_PROVIDER消耗更少的内存。 对NETWORK_PROVIDER
使用此权限 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />