标签: android-location

Android 位置有时太旧

当我尝试在 android 中获取设备位置时遇到一个奇怪的问题。

有时它可以很好地获取当前位置,但有时它会不断返回旧位置。我所说的“旧”指的是几天前的情况,距离它应该所在的位置数十甚至数百公里。

我不知道我做错了什么,我尝试了几种不同的方法来获取位置,但似乎每种方法都有相同的问题。

最奇怪的是,它并不是在所有设备上都会发生……或者至少看起来是这样。三星 S3 和 S4 受影响最大,但在我的 Nexus 4 中从未发生过。

这是我的代码,也许你发现有问题:

    public void startSearchingLocation() {

        // Define a listener that responds to location updates
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location
                // provider.


                makeUseOfNewLocation(location);

            }

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

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }

        }; …
Run Code Online (Sandbox Code Playgroud)

android android-location

5
推荐指数
1
解决办法
3495
查看次数

仅针对特定应用程序的模拟位置

我只想为特定应用程序设置模拟位置。到目前为止,我所理解的是,如果我设置 GPS 提供商一些模拟位置,那么所有通过 GPS 访问位置的应用程序都将收到模拟位置。可以使其成为特定于应用程序的吗?(意味着只有特定的应用程序应该看到模拟的位置,所有其他应用程序都会看到真实的位置。)如果是,如何?

gps android android-location

5
推荐指数
0
解决办法
730
查看次数

如何检查位置设置更改后是否失败

我正在创建 LocationRequest 类的 locationrequest 对象,其方法用于确定我的应用程序所需的位置准确度级别。

private LocationRequest mLocationRequest;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(2000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
Run Code Online (Sandbox Code Playgroud)

然后创建一个 LocationSettingsRequest.Builder 对象,然后向其中添加位置请求对象。

new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
Run Code Online (Sandbox Code Playgroud)

根据 Android 文档,SettingsClient 负责确保设备的系统设置针对应用程序的位置需求进行了正确配置。

SettingsClient client = LocationServices.getSettingsClient(this);
Task<LocationSettingsResponse> task = 
client.checkLocationSettings(builder.build());
Run Code Online (Sandbox Code Playgroud)

文档指出,当任务完成时,客户端可以通过查看 LocationSettingsResponse 对象的状态代码来检查位置设置。

task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() 
{
            @Override
            public void onComplete(Task<LocationSettingsResponse> task) {
                try {
                    LocationSettingsResponse response = task.getResult(ApiException.class);

                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                } catch (ApiException exception) {
                    Log.v(" Failed ", String.valueOf(exception.getStatusCode()));

                    switch (exception.getStatusCode()) {

                        case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                            // Location …
Run Code Online (Sandbox Code Playgroud)

android android-location

5
推荐指数
1
解决办法
2496
查看次数

GPS定位启用后如何立即获取位置

我正在实现一个模块,需要用户的纬度经度来离线打卡考勤。

我已经按照LINK上的示例实现了GPSTracker类

但是启用 GPS 定位后,我正在打卡出勤,那么此类将返回空位置对象。但3060秒后它会正确返回位置对象。

我还在清单中添加了COARSE权限FINE,并获得了运行时权限。

所以我需要帮助如何在启用 GPS 定位后立即获取纬度和经度。

android android-maps android-location

5
推荐指数
1
解决办法
1711
查看次数

为什么模拟位置会跳回真实位置

我添加了一个测试提供者,使用LocationManager.GPS_PROVIDER提供者名称,如此处所述

https://mobiarch.wordpress.com/2012/07/17/testing-with-mock-location-data-in-android/

在 Google 地图应用程序中,我看到位置在模拟位置和真实位置之间跳转,然后又返回模拟位置。

为什么会跳转到真实位置?我该如何阻止它?

也许我误解了如何使用模拟位置。我还没有找到任何可用的官方文档。

android android-location

5
推荐指数
1
解决办法
2万
查看次数

如何检测用户是否在设置中关闭了位置信息?

我想检测用户是否在运行时关闭了该位置。我可以检查他是否打开了它,或者在应用程序启动之前用户是否关闭了位置,但我无法检查他是否在之后关闭了它。

代码示例: MapEntity扩展LocationListener

class MapViewer(a: MainActivity, parentView: ViewGroup) : MapEntity(a, parentView) {

    override fun onProviderEnabled(provider: String?) {
        activity.hideGpsSnackbar()
    }

    override fun onProviderDisabled(provider: String?) {
        activity.showGpsSnackbar()
    }

}
Run Code Online (Sandbox Code Playgroud)

对于实时 GPS 位置检查,我正在使用 GnssStatus.Callback()

更新:

我已经BroadcastReceiver根据下面的答案创建了。

abstract class GPSReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context, intent: Intent) {
        try {
           val locationManager = context.getSystemService(LOCATION_SERVICE) as LocationManager

             if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    onGpsChanged(true)
                } else {
                    onGpsChanged(false)
                }
            } catch (ex: Exception) {
                App.log("IsGPSEnabled: $ex")
            }

        }

        abstract fun onGpsChanged(isEnabled: Boolean)
    } …
Run Code Online (Sandbox Code Playgroud)

android android-location kotlin android-maps-v2 android-gps

5
推荐指数
1
解决办法
1882
查看次数

SettingsClient.checkSettings 始终失败,状态为 LocationSettingsStatusCodes.RESOLUTION_REQUIRED

我正在我的应用程序中使用LocationServices。在使用位置服务之前,我尝试验证具有所需设置的位置是否已打开,但我面临的问题SettingsClient.checkSettings始终是failing。请参阅我LocationRequestLocationSettingRequest建造者:

位置请求

 mLocationRequest = LocationRequest()
        mLocationRequest.interval = interval
        mLocationRequest.fastestInterval = interval

        Log.v(TAG, "distance => $distance")
        if(distance > 0) {
            mLocationRequest.smallestDisplacement = distance
        }

        mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY

        buildLocationSettingsRequest()
Run Code Online (Sandbox Code Playgroud)

位置设置RequestBuilder

private fun buildLocationSettingsRequest() {
        val builder = LocationSettingsRequest.Builder()
        builder.addLocationRequest(mLocationRequest)
        mLocationSettingsRequest = builder.build()

        mSettingsClientInit = true
    }
Run Code Online (Sandbox Code Playgroud)

checkSettings要求

mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
                .addOnSuccessListener(this, object : OnSuccessListener<LocationSettingsResponse> {

                    override fun onSuccess(locationSettingsResponse: LocationSettingsResponse) {
                        Log.i(TAG, "LocationManager: All location settings are satisfied.");
                        mLocationCallback?.let {
                            fusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); …
Run Code Online (Sandbox Code Playgroud)

android android-location kotlin google-play-services

5
推荐指数
1
解决办法
1379
查看次数

locationListener 在前台服务 30 秒后不起作用

我创建了一个服务,用于查找用户的坐标并将其存储在 SQLite 数据库中。

public class GPS_Service extends Service {

DatabaseHelper myDb;

private LocationListener locationListener;
private LocationManager locationManager;

private String latitude, longitude;

@Override
public void onCreate() {
    super.onCreate();

    myDb = new DatabaseHelper(this);

}

@SuppressLint("MissingPermission")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Intent notificationIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, 0);

    Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("Service")
            .setContentText("Coordinates Location Running")
            .setContentIntent(pendingIntent)
            .build();

    startForeground(1, notification);

    locationListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) { …
Run Code Online (Sandbox Code Playgroud)

android android-location foreground-service android-10.0

5
推荐指数
1
解决办法
1673
查看次数

如何使用 Geolocator 插件检测 Flutter 中的模拟位置

我正在尝试检测模拟位置。我已经安装了假位置应用程序并在“设置”(在“开发人员选项”下)中启用了“模拟位置”。但是当我尝试使用Geolocator检测模拟位置时,它根本不起作用。

Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;

serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
  return Future.error('Location services are disabled.');
}

permission = await Geolocator.checkPermission();
if (permission == LocationPermission.deniedForever) {
  return Future.error(
      'Location permissions are permanently denied, we cannot request permissions.');
}

if (permission == LocationPermission.denied) {
  permission = await Geolocator.requestPermission();
  if (permission != LocationPermission.whileInUse &&
      permission != LocationPermission.always) {
    return Future.error(
        'Location permissions are denied (actual value: $permission).');
  }
}
return await Geolocator.getCurrentPosition(
    desiredAccuracy: LocationAccuracy.high);
} …
Run Code Online (Sandbox Code Playgroud)

android geolocation android-location flutter

5
推荐指数
0
解决办法
1195
查看次数

如何检查LocationManager.NETWORK_PROVIDER是否可用?

如何检查是否LocationManager.NETWORK_PROVIDER可用?

我已启用AndroidManifest.xml但我需要在代码中检查它,如果无法使用GPS_PROVIDER.有人能帮我吗 ?

android android-location

4
推荐指数
1
解决办法
2万
查看次数