一个很长的 gps 位置搜索

A very long gps location search

我正在编写一个使用 GPS 获取用户位置的应用程序。我添加了清单 (android.permission.ACCESS_FINE_LOCATION) 的权限,要求获得所需的运行时权限并让我的应用程序开始搜索,但此搜索可能需要很长时间(大约 5 分钟)或永远持续不返回任何结果。但是原来的 Google 地图应用程序会立即确定我的位置,所以问题不在 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 定位。如果您在构建内部测试您的代码,这可能是不可能的,或者可能需要更长的时间。 Google 地图(很可能)使用融合位置 api,它利用了许多不同的位置来源 - 但它提供了位置的准确性,因此您可以确定准确性是否足以满足您的需求(google 地图以您所在位置周围较大/较小的圆圈显示此精度)。

要使用融合位置 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