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

j4r*_*rey 5 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");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

我确实收到一条日志消息,上面写着“位置被模拟”。

我的“提供者”错了吗?

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

小智 4

看起来 FusedLocationProvider 的时钟与系统时钟不同。那就elapsedRealTimeNanos不同了System.nanoTime。因此,您使用系统时钟设置的时间将关闭,并且 I\xe2\x80\x99m 假设新位置被认为是陈旧且无效的。

\n\n

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

\n\n
Location current = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);\nnanoOffset = current.getElapsedRealtimeNanos() - System.nanoTime();\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后,我创建的完整模拟位置如下:

\n\n
Location location = new Location("flp");  // indicates mock \nlocationlocation.setLatitude(latLon.getLat());\nlocation.setLongitude(latLon.getLon());\nlocation.setTime(System.currentTimeMillis());\nlocation.setAccuracy(6f);\nlocation.setElapsedRealtimeNanos(System.nanoTime() + nanoOffset);\n
Run Code Online (Sandbox Code Playgroud)\n