Android模拟位置不适用于谷歌地图

时间:2013-02-25 16:08:55

标签: android mocking maps location

我创建了一个模拟gps位置的Android应用程序,它适用于大多数程序,如igo,waze,osmAnd,GPS状态等,但谷歌地图仍然显示我所处的实际位置。

我也禁用了gps和网络位置。

任何想法为什么会这样?怎么解决?谢谢

我使用的代码:

public void setLocation(double latitude, double longitude, double altitude, float bearing, float speed){
    String PROVIDER_NAME = LocationManager.GPS_PROVIDER;
    locationManager.addTestProvider(PROVIDER_NAME, false, false, false, false, true, true, true, Criteria.POWER_LOW, Criteria.ACCURACY_FINE);

    locationManager.setTestProviderEnabled(PROVIDER_NAME, true);
    locationManager.setTestProviderStatus(PROVIDER_NAME, LocationProvider.AVAILABLE, null, System.currentTimeMillis());

    Location location = new Location(PROVIDER_NAME);
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    location.setAltitude(altitude);
    location.setBearing(bearing);
    location.setSpeed(speed);   
    location.setTime(System.currentTimeMillis());
    locationManager.setTestProviderLocation(PROVIDER_NAME, location.getLocation());
}

2 个答案:

答案 0 :(得分:2)

您应该使用GoogleMap.setLocationSource(LocationSource source)为Android上的最新Google Maps SDK提供随机位置。使用这样的随机位置源:

// In your MapFragment implementation or similar
if (BuildConfig.DEBUG)
    map.setLocationSource(new MockLocationSource());

MockLocationSource.java:
public class MockLocationSource implements LocationSource {

    private static final float ACCURACY = 1; // Meters
    private static final int MAX_SPEED = 10; // m/s
    private static final LatLng CENTER = new LatLng(59.91127, 10.70834);
    private static final double DELTA_LAT = 0.0005;
    private static final double DELTA_LON = 0.0005;

    private static final long UPDATE_PERIOD = TimeUnit.SECONDS.toMillis(2);

    private final Handler handler = new Handler();
    private LatLng lastCoordinate = CENTER;
    private OnLocationChangedListener listener;

    private void scheduleNewFix() {
        handler.postDelayed(updateLocationRunnable, UPDATE_PERIOD);
    }

    private final Runnable updateLocationRunnable = new Runnable() {

        @Override
        public void run() {
            Location randomLocation = generateRandomLocation();
            listener.onLocationChanged(randomLocation);
            scheduleNewFix();
        }
    };

    public Location generateRandomLocation() {

        Location location = new Location(getClass().getSimpleName());
        location.setTime(System.currentTimeMillis());
        location.setAccuracy(ACCURACY);
        location.setBearing(randomizer.nextInt(360));
        location.setSpeed(randomizer.nextInt(MAX_SPEED));
        location.setLatitude(lastCoordinate.latitude + scaleOffset(DELTA_LAT));
        location.setLongitude(lastCoordinate.longitude + scaleOffset(DELTA_LON));

        lastCoordinate = new LatLng(location.getLatitude(), location.getLongitude());

        return location;
    }

    private final static Random randomizer = new Random();

    private double scaleOffset(double value) {
        return (randomizer.nextDouble() - 0.5) * value;
    }

    @Override
    public void activate(OnLocationChangedListener locationChangedListener) {
        listener = locationChangedListener;
        scheduleNewFix();
    }

    @Override
    public void deactivate() {
        handler.removeCallbacks(updateLocationRunnable);
    }

}

答案 1 :(得分:1)

感谢您的帮助。 我发现了问题,当其他人的应用程序不需要时,护目镜似乎需要设置准确度 所以添加一行location.setAccuracy(1);它解决了这个问题。