- 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
}
}
1条答案
按热度按时间x8diyxa71#
像这样的怎么样。
外推是在路线的2个点之间线性计算的(在纬度和经度上)。