我有一個CLLocation
對象的數組,我從文件中解析出來。我想模擬用戶沿着這條路線移動,我已經實現了這一點:試圖模擬MapView中的路由
for (CLLocation *loc in simulatedLocs) {
[self moveUser:loc];
sleep(1);
}
這就是所謂的循環方法:在
- (void)moveUser:(CLLocation*)newLoc
{
CLLocationCoordinate2D coords;
coords.latitude = newLoc.coordinate.latitude;
coords.longitude = newLoc.coordinate.longitude;
CustomAnnotation *annotation = [[CustomAnnotation alloc] initWithCoordinate:coords];
annotation.title = @"User";
// To remove the previous location icon
NSArray *existingpoints = self.mapView.annotations;
if ([existingpoints count] > 0) {
for (CustomAnnotation *annotation in existingpoints) {
if ([annotation.title isEqualToString:@"User"]) {
[self.mapView removeAnnotation:annotation];
break;
}
}
}
MKCoordinateRegion region = { coords, {0.1, 0.1} };
[self.mapView setRegion:region animated:NO];
[self.mapView addAnnotation: annotation];
[self.mapView setCenterCoordinate:newLoc.coordinate animated:NO];
}
但只有最後一個位置在運行iPhone模擬器時,數組及其區域顯示在mapView中。我想模擬用戶每1秒「移動」,我怎麼能這樣做?
謝謝!
不要像那樣使用'sleep'。 –