gpt4 book ai didi

java - 很长的 GPS 位置搜索

转载 作者:行者123 更新时间:2023-12-02 11:09:57 24 4
gpt4 key购买 nike

我正在编写一个通过 GPS 获取用户位置的应用程序。我向 list 添加了权限(android.permission.ACCESS_FINE_LOCATION),请求所需的运行时权限并使我的应用开始搜索,但此搜索可能需要很长时间(大约 5 分钟)或永远持续不下去返回任何结果。但原来的谷歌地图应用程序会立即确定我的位置,所以问题不在于 GPS。也许有人知道问题是什么?

这是我的代码:

public class MainActivity extends AppCompatActivity
implements ActivityCompat.OnRequestPermissionsResultCallback {

private static final int PERMISSION_REQUEST_GPS = 0;

LocationManager manager;

private LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
if (location != null) {
String lathitude = String.valueOf(location.getLatitude());
String longitude = String.valueOf(location.getLongitude());
Toast.makeText(getApplicationContext(),
"Your Location is - \nLat: " + lathitude + "\nLong: " + longitude,
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Sorry, location unavailable",
Toast.LENGTH_LONG).show();
}
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btnShowLocation).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findGPSLocation();
}
});
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_GPS) {
// Request for camera permission.
if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission has been granted.
startGPSSearch();
} else {
// Permission request was denied.
}
}
}

private void findGPSLocation() {
// Check if the Camera permission has been granted
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
// Permission is already available
startGPSSearch();
} else {
// Permission is missing and must be requested.
requestPermission();
}
}

private void requestPermission() {
// Permission has not been granted and must be requested.
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_GPS);
}

private void startGPSSearch() {
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
}

最佳答案

您请求 GPS 位置,这意味着 Android 正在等待卫星的 GPS 修复。如果您在构建内测试代码,这可能是不可能的,或者可能需要更长的时间。谷歌地图(最有可能)使用融合位置 API,它利用许多不同的位置源 - 但它提供了位置的准确性,因此您可以决定准确性是否足以满足您的需求(谷歌地图以更大的值显示此准确性)/围绕您的位置的较小圆圈)。

要使用融合位置 API,请从此处开始:https://developers.google.com/location-context/fused-location-provider/

您可以通过添加以下内容来请求网络位置更新:

manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);

请参阅此处:https://developer.android.com/guide/topics/location/strategies

关于java - 很长的 GPS 位置搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50677938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com