2

我创建了一个模拟 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());
}
4

2 回答 2

3

您应该使用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);
    }

}
于 2013-05-29T01:53:52.330 回答
1

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

于 2013-03-03T20:58:52.273 回答