在MKMapView中围绕用户位置绘制一个1000米半径的圆 [英] Draw a circle of 1000m radius around users location in MKMapView
问题描述
(使用iOS 5和Xcode 4.2)
我有一个MKMapView,想在用户位置周围画一个1000米半径的圆圈。
I have an MKMapView and want to draw a circle of 1000m radius around the user location.
从表面上看,似乎实现了 mapView:viewForAnnotation:地图视图委托方法,并为用户位置添加自定义MKAnnotationView,完美的解决方案。它看起来像这样:
On the surface it would seem that implementing the mapView:viewForAnnotation: map view delegate method, and adding a custom MKAnnotationView for the users location, would be a perfect solution. It would look something like this:
- (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;
}
}
但是,当您使用地图上的注释时,放大和缩小地图。
However annotations on the map don't scale when you zoom in and out of the map.
所以我尝试使用 MKCircle 类,并将其坐标设置为来自locationManger / map视图委托的最新坐标。但是,作为坐标属性 MKCircle是readonly,我不得不删除叠加层,然后每次用户移动时添加一个新叠加层。发生了明显的闪烁。
So I tried adding an overlay (because overlays scale with the map), using the MKCircle class and setting its co-ordinates to the latest co-ordinates from my locationManger/map view delegate. However as the coordinate property of MKCircle is readonly, I'm having to remove the overlay then add a new one each time the user moves. Causing a noticeable flicker as it happens.
当地图视图缩小和缩小时,有没有办法无缝地进行注释缩放?或者是否有一种很好的方法可以使叠加层随着用户位置的变化无缝移动?
Is there any way to make an annotation scale seamlessly as the map view is scaled in and out? Or is there a good way to make an overlay move seamlessly with changes in the users location?
我将非常感谢您的帮助:)
I would be very grateful for your help :)
推荐答案
尝试自定义叠加层。在viewDidLoad中添加:
Try a custom overlay. Add this in viewDidLoad:
MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];
userLocation可以通过将MKUserLocationAnnotation存储为属性来获得。然后,要实际绘制圆圈,将其放在地图视图的委托中:
userLocation can be obtained by storing the MKUserLocationAnnotation as a property. Then, to actually draw the circle, put this in the map view's delegate:
- (MKOverlayRenderer *)mapView:(MKMapView *)map viewForOverlay:(id <MKOverlay>)overlay
{
MKCircleRenderer *circleView = [[MKCircleRenderer alloc] initWithOverlay:overlay];
circleView.strokeColor = [UIColor redColor];
circleView.fillColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
return circleView;
}
这篇关于在MKMapView中围绕用户位置绘制一个1000米半径的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!