如何让我的自定义圈子叠加作为子视图实时显示当前用户位置?我设法显示我的圆形叠加并进入用户位置的中心,但是当用户移动(gps)时,叠加层保持静止并且不跟随用户。我该如何解决这个问题?提前谢谢..
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setRegion:MKCoordinateRegionMake(self.location.coordinate, MKCoordinateSpanMake(0.02, 0.02))];
[self configureOverlay];
[[self locationManager] startUpdatingLocation];
}
在configureOverlay中:
CircleOverlay *overlay = [[CircleOverlay alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addOverlay:overlay];
GeoQueryAnnotation *annotation = [[GeoQueryAnnotation alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addAnnotation:annotation];
我也尝试过使用:
CLLocation *location = _locationManager.location;
CLLocationCoordinate2D coordinate = [location coordinate];
并用self.location.coordinate替换坐标
任何建议都将不胜感激。谢谢你
答案 0 :(得分:1)
告诉你的课你要实现CLLocationManagerDelegate
@interface YourClass () <CLLocationManagerDelegate>
然后添加以下方法:
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//You can change the overlay coordinates here. Even with an animation if you want.
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ? @"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting Location"message:errorType delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
[alert show];
}
每次位置管理器检测到用户位置发生变化时,都会触发locationManager:didUpdateToLocation ...方法。
干杯。
修改强> 我忘记了非常重要的事情。在viewDidLoad方法中,将您的类添加为委托:
self.locationmanager.delegate = self;