objective-c - 圆圈叠加跟随用户位置

标签 objective-c ios xcode location overlay

如何使我的自定义圆圈叠加层作为 subview 实时跟随当前用户位置?我已成功显示我的圆圈叠加层并到达用户位置的中心,但是当用户移动(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 替换坐标

如有任何建议,我们将不胜感激。谢谢

最佳答案

告诉你的类(class)你将实现 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 方法中添加您的类作为委托(delegate):

self.locationmanager.delegate = self;

关于objective-c - 圆圈叠加跟随用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12639353/

相关文章:

ios - 一张带有 alpha 滞后 spritekit 的图像

objective-c - UISplitViewController:如何以编程方式导出详细 View 的宽度(以像素为单位)?

objective-c - 在 swift 中子类化 'PDFView' - 找不到接口(interface)声明

ios - NSLayoutConstraint 中断

c++ - 是否可以在 .metal 文件中包含标准 C++ 库?

ios - 在 Xcode (6.1) 中设置文本样式

iphone - 嵌入式视频播放多个方向?

iOS NSRegularExpression 如何找到模式的第一个匹配 "TAIL",如 : HEAD(. *)TAIL

ios - 如何在 Apple Watch 中集成苹果 map ?

ios - Swift 3.0 CIEdgeWork 过滤器不工作