4

我正在使用 LocationClient 效果很好。现在我正在尝试创建模拟位置(setMockMode(true)+ setMockLocation(mockLoc)。但是我的 LocationListener 的 onLocationChanged 没有被调用。可能是什么问题?

我跟着这个:http: //developer.android.com/training/location/location-testing.html

脚步:

  • 连接
  • 请求位置更新
  • setMockMode 真
  • setMockLocation (provider = "flp")
4

1 回答 1

7

Okay so you gotta update your Locations with setTime() and setElapsedRealtimeNanos().

A complete create method for a location will look something like following:

@SuppressLint("NewApi")
public Location createLocation(double lat, double lng, float accuracy) {
    // Create a new Location
    Location newLocation = new Location(PROVIDER);
    newLocation.setLatitude(lat);
    newLocation.setLongitude(lng);
    newLocation.setAccuracy(accuracy);
    newLocation.setTime(System.currentTimeMillis());

    int sdk = android.os.Build.VERSION.SDK_INT;
    if(sdk >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
        newLocation.setElapsedRealtimeNanos(
             SystemClock.elapsedRealtimeNanos());
    }
    return newLocation;
}

This has been tested to work with Nexus 5.

于 2014-06-07T08:35:06.557 回答