android 如何模拟多个坐标之间的移动位置?

wrrgggsh  于 2023-01-19  发布在  Android
关注(0)|答案(1)|浏览(107)
    • bounty将在5天后过期**。回答此问题可获得+150声望奖励。Ynemp Kski希望引起更多人关注此问题。

如何使模拟位置以一定的速度在多个坐标之间移动?
示例:

Coordinate 1: 52.520901, 13.370790
Coordinate 2: 52.522351, 13.367990
Coordinate 3: 52.524080, 13.367196

代码应该能够模拟位置从坐标1到2到3以20公里/小时的恒定速度...这是我最好的尝试,但它对位置没有影响。我真的很感激一些帮助,谢谢。

public void simulate(Context context) {
    try {
        // Define an array of coordinates and a speed in km/h
        double[][] coordinates = {{52.520901, 13.370790}, {52.522351, 13.367990}, {52.524080, 13.367196}};
        double speed = 15.0; // km/h

        // Create a new FusedLocationProviderClient
        FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);

        // Create a new LocationRequest
        LocationRequest locationRequest = new LocationRequest();
        locationRequest.setInterval(1000);
        locationRequest.setFastestInterval(1000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        // Loop through the coordinates and simulate movement
        for (int i = 0; i < coordinates.length; i++) {
            // Create a new Location object
            Location location = new Location("mock");
            location.setLatitude(coordinates[i][0]);
            location.setLongitude(coordinates[i][1]);
            location.setAccuracy(2.0f);
            location.setTime(System.currentTimeMillis());

           location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                location.setBearingAccuracyDegrees(0.1f);
                location.setVerticalAccuracyMeters(0.1f);
                location.setSpeedAccuracyMetersPerSecond(0.01f);
            }

            // Set the location on the device
            fusedLocationClient.setMockMode(true);
            fusedLocationClient.setMockLocation(location);
            fusedLocationClient.requestLocationUpdates(locationRequest,new LocationCallback(), Looper.getMainLooper());
            // Sleep for the appropriate amount of time based on the speed
            int sleepTime = (int) (3600.0 / speed * 1000.0);
            Thread.sleep(sleepTime);
        }
    } catch (InterruptedException e) {
        // Handle Interrupted exception
    }
}
x8diyxa7

x8diyxa71#

像这样的怎么样。

public class Location {
    private static final double KILOMETERS_PER_NAUTICAL_MILE = 1.852;
    final String name;
    final double longitude;
    final double latitude;

    /**
     * Creates a (geo-location) with latitude and longitude
     * @param name the name of the location
     * @param latitude in degrees
     * @param longitude in degrees
     */
    public Location(String name, double latitude, double longitude) {
        this.name = name;
        this.latitude  = latitude;
        this.longitude = longitude;
    }

    /**
     * Computes the distance between this location and another location
     * measured in kilometers
     */
    public double distanceTo(Location that) {
        double lat1 = Math.toRadians(this.latitude);
        double lon1 = Math.toRadians(this.longitude);
        double lat2 = Math.toRadians(that.latitude);
        double lon2 = Math.toRadians(that.longitude);

        // great circle distance in radians, using law of cosines formula
        double angle = Math.acos(Math.sin(lat1) * Math.sin(lat2)
                               + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon1 - lon2));

        // each degree on a great circle of Earth is 60 nautical miles
        double nauticalMiles = 60 * Math.toDegrees(angle);
        return KILOMETERS_PER_NAUTICAL_MILE * nauticalMiles;
    }

    // return string representation of this point
    public String toString() {
        return name + " (" + latitude + ", " + longitude + ")";
    }
}
public class Route {
    private List<Location> points = new ArrayList<>();

    public void add(Location location) {
        points.add(location);
    }

    /**
     * Computes the location (by extrapolation) on the route after a given time at a
     * given speed
     * 
     * @param speed      constant speed (kilometers/hour)
     * @param elapseTime elapse time (seconds)
     * @return the extrapolated location on the route
     */
    public Location computeLocation(double speed, long elapseTime) {
        return computeLocation(speed * elapseTime / 3600d)
    }

    /**
     * Computes the location (by extrapolation) on the route after traveling a given distance
     * 
     * @param traveledDistance      traveled distance (kilometers)
     * @return the extrapolated location on the route
     */
    public Location computeLocation(double traveledDistance) {
        for (int i = 1; i < points.size(); i++) {
            Location start = points.get(i - 1);
            Location end = points.get(i);
            double dist = start.distanceTo(end);
            if (dist < traveledDistance) {
                // continue to the next segment
                traveledDistance -= dist;
            } else {
                // the final location is somewhere on current segment
                double ratio = traveledDistance / dist;
                return new Location(
                    "somewhere between " + start.name + " and " + end.name, 
                    start.latitude * (1.0 - ratio) + end.latitude * ratio, 
                    start.longitude * (1.0 - ratio) + end.longitude * ratio);
            }
        }
        // we reached the end of the route (traveled distance is > route length)
        return points.get(points.size() - 1);
    }
}

外推是在路线的2个点之间线性计算的(在纬度和经度上)。

相关问题