如何实现模拟位置



我需要在没有GPS的情况下为我的设备使用假GPS坐标。我正在尝试使用此手册:http://developer.android.com/training/location/location-testing.html

但是导入com.google.android.gms.location.LocationClient不可用。据我所知,它已被弃用。但是现在如何实现模拟位置?

请参阅此处: FusedLocationProviderApi

进一步回答迈克尔。

  1. Init GoogleApiClient:

    private GoogleApiClient mGoogleApiClient;
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    
  2. 连接客户端:

    mGoogleApiClient.connect();
    
  3. 设置新位置:

    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(48.446743);
    location.setLongitude(52.44672);
    location.setAccuracy(4.0f);
    location.setElapsedRealtimeNanos(elapsedTimeNanos);
    location.setTime(currentTime);
    LocationServices.FusedLocationApi.setMockLocation(mGoogleApiClient, location);
    
  4. 完成后,需要断开客户端的连接:

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    

最新更新