从谷歌商店下载并使用“假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类来处理所有函数。 流程是初始化 - &gt; connect-&gt; startmock-&gt; mock-&gt; stopmock-&gt; disconnect
如果我执行connect(),则locationchange()为我提供当前位置,但是当我使用startmock()和domock()时,locationchange()暂停并且不返回任何内容,并且当我stopmock()时,locationchange()开始给我当前的位置位置。
我收到一条日志消息,上面写着'location mocked'。
我的'提供者'错了吗?
我希望能够创建一个模拟位置,并能够访问谷歌地图并看到它指向模拟位置。
答案 0 :(得分:1)
看起来FusedLocationProvider与系统时钟的时钟不同。即elapsedRealTimeNanos
与System.nanoTime
不同。因此,您使用系统时钟设置的时间将关闭,我假设新的位置被视为过时且无效。
要获得有效的elapsedRealTimeNanos
,每次创建模拟位置时,我都会向System.nanoTime()
添加偏移量。我第一次连接到googleApiClient时计算出偏移量:
Location current = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
nanoOffset = current.getElapsedRealtimeNanos() - System.nanoTime();
然后,我创建的完整模拟位置如下:
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);