【问题标题】:Circle overlay follow user location圆圈覆盖跟随用户位置
【发布时间】:2012-09-28 11:42:43
【问题描述】:

如何让我的自定义圆圈叠加层作为子视图实时跟随当前用户位置?我已经设法显示我的圆圈覆盖并到达用户位置的中心,但是当用户移动(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 替换坐标

任何建议将不胜感激。谢谢您

【问题讨论】:

    标签: objective-c ios xcode location overlay


    【解决方案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;
    

    【讨论】:

    • 好的..虽然这可行,谢谢..它似乎一直在闪烁(更新位置)。有没有办法让它更高效,看起来不像闪烁?
    • @snksnk 您可以尝试两件事:首先,检查位置是否与以前相同,在这种情况下不要更新它。另外,您可以尝试使用动画来执行坐标的更改,使用developer.apple.com/library/ios/documentation/uikit/reference/…:如果我的回答有效,请接受。我鼓励您就动画问题提出另一个问题。将您用于更新视图的代码放在那里,让人们帮助您:) 干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-30
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多