android模拟位置不适用于locationservices.fusedlocationapi

标签 android location google-api-client fusedlocationproviderapi

从谷歌商店下载并使用“Fake Gps”后,我想创建一个用于教育目的。

我正在使用 GoogleApiClient FusedLocationAPI 获取我的当前位置并创建模拟位置。我能够获取我当前的位置,但无法模拟位置。

public class GoogleApiClientGeoLocation implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
    private GoogleApiClient googleApiClient;

    public GoogleApiClientGeoLocation(Context activity) {
        googleApiClient = new GoogleApiClient.Builder(activity, this, this).addApi(LocationServices.API).build();
        ....
    }

    public void connect() {
        if (googleApiClient != null) {
           googleApiClient.connect();
        }
    }

    public void disconnect() {
        googleApiClient.disconnect();
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.e("TAG","Location changed"+location.getLatitude() + ", "+ location.getLongitude());
    }

    private static final String PROVIDER = LocationManager.NETWORK_PROVIDER;//"flp";
    private static final double LAT = 37.377166;
    private static final double LNG = -122.086966;
    private static final float ACCURACY = 3.0f;

    public Location createLocation(double lat, double lng, float accuracy) { //<-for testing purposes using a static lat,long
        // Create a new Location
        Location newLocation = new Location(PROVIDER);
        newLocation.setLatitude(LAT);
        newLocation.setLongitude(LNG);
        newLocation.setAltitude(1000);
        newLocation.setElapsedRealtimeNanos(System.nanoTime());
        newLocation.setTime(System.currentTimeMillis());
        newLocation.setAccuracy(500);
        return newLocation;
    }

    public void startMock() {
        LocationServices.FusedLocationApi.setMockMode(googleApiClient, true);
    }
    public void stopMock() {
        LocationServices.FusedLocationApi.setMockMode(googleApiClient, false);
    }
    public void domock() {
        Location testLocation = createLocation(LAT, LNG, ACCURACY);
        if (ContextCompat.checkSelfPermission(_context, android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {

            LocationServices.FusedLocationApi.setMockLocation(googleApiClient, testLocation);
            Log.e("TAG","Location Mocked");
        }else{
            Log.e("TAG","Can not Mock location");
        }
    }
}

我有一个处理所有功能的 GoogleApiClientGeoLocation 类。 流程是初始化->connect->startmock->mock->stopmock->disconnect

如果我执行 connect(),locationchange() 会给我当前位置,但是当我 startmock() 和 domock() locationchange() 暂停并返回任何内容时,当我 stopmock() locationchange() 开始给我当前位置时再次定位。

我确实收到一条日志消息,上面写着“location mocked”。

我的'PROVIDER'错了吗?

我希望能够创建一个模拟位置并能够转到谷歌地图并看到它指向模拟位置。

最佳答案

看起来 FusedLocationProvider 的时钟与系统时钟不同。 IE。 elapsedRealTimeNanosSystem.nanoTime 不同。因此,您使用系统时钟设置的时间将关闭,我假设新位置被认为是陈旧和无效的。

为了获得有效的 elapsedRealTimeNanos,我在每次创建模拟位置时都向 System.nanoTime() 添加了一个偏移量。我在第一次连接到 googleApiClient 时计算该偏移量:

Location current = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
nanoOffset = current.getElapsedRealtimeNanos() - System.nanoTime();

那么,我创建的完整mock Location如下:

Location location = new Location("flp");  // indicates mock 
locationlocation.setLatitude(latLon.getLat());
location.setLongitude(latLon.getLon());
location.setTime(System.currentTimeMillis());
location.setAccuracy(6f);
location.setElapsedRealtimeNanos(System.nanoTime() + nanoOffset);

关于android模拟位置不适用于locationservices.fusedlocationapi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38673666/

相关文章:

android - 如何在 FCM 通知中创建主题

iphone - iOS 5 之前的定位服务和设置

android - "The current user has insufficient permissions to perform the requested operation.” - Google Play 开发者 API

android - 访问 Activity :List from the GoogleApiClient

java - 没有这样的表 - 在 SQLite Android 中插入值时出错

java - 从自定义数组列表创建一个新列表

java - AES 256-CBC 上的 key 和 iv 问题

postgresql - Postgis最近的坐标

android - Android 位置监控中的误报

python - 使用 googleapiclient 通过 Id 发送电子邮件草稿