(使用iOS 5和Xcode 4.2)
我有一个MKMapView,想在用户位置周围绘制一个1000米半径的圆.
从表面上看,似乎实现mapView:viewForAnnotation: map view delegate方法,并为用户位置添加自定义MKAnnotationView,将是一个完美的解决方案.它看起来像这样:
- (MKAnnotationView *)mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>)annotation
{
// If it's the user location, return my custom MKAnnotationView.
if ([annotation isKindOfClass:[MKUserLocation class]]) {
return myCustomAnnotationView;
} else {
return nil;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当您放大和缩小地图时,地图上的注释不会缩放.
所以我尝试添加叠加层(因为叠加层与地图一起缩放),使用MKCircle类并将其坐标设置为来自locationManger/map视图委托的最新坐标.但是由于MKCircle 的坐标属性是readonly,我不得不删除叠加层,然后每次用户移动时添加一个新叠加层.当它发生时引起明显的闪烁.
当地图视图缩小和缩小时,有没有办法无缝地制作注释比例?或者是否有一种很好的方法可以使叠加层随着用户位置的变化无缝移动?
我非常感谢你的帮助:)
我有一个MKMapView (self.mapView),其中添加了具有特定半径 (self.location.radius) 的MKCircle覆盖 (self.radiusOverlay)。
MKMapView正下方有一个UISlider (self.radiusSlider),它可以在Value Changed上更改叠加层的半径:
- (void)viewWillAppear:(BOOL)animated {
self.radiusOverlay = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake([self.location.latitude floatValue], [self.location.longitude floatValue]) radius:[self.location.radius floatValue]];
[self.mapView addOverlay:self.radiusOverlay];
self.radiusSlider.value = [self.location.radius floatValue];
}
Run Code Online (Sandbox Code Playgroud)
这是进行半径更新的方法:
- (IBAction)updateLocationRadius:(UISlider *)sender {
[self.mapView removeOverlay:self.radiusOverlay];
self.location.radius = [NSNumber numberWithFloat:sender.value];
self.radiusOverlay = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake([self.location.latitude floatValue], [self.location.longitude floatValue]) radius:[self.location.radius floatValue]];
[self.mapView addOverlay:self.radiusOverlay];
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,但看起来不太好:通过删除并重新添加self.radiusOverlay我导致覆盖层闪烁。
我该怎么做才能避免这种闪烁?