在 MKMapView 中围绕用户位置画一个半径为 1000m 的圆

时间:2023-03-24
本文介绍了在 MKMapView 中围绕用户位置画一个半径为 1000m 的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

(使用 iOS 5 和 Xcode 4.2)

我有一个 MKMapView,想在用户位置周围画一个半径为 1000m 的圆.

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 是只读的,我必须删除覆盖,然后在每次用户移动时添加一个新的.发生时会引起明显的闪烁.

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?

非常感谢您的帮助:)

推荐答案

尝试自定义叠加层.在 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 中围绕用户位置画一个半径为 1000m 的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用新的外部存储标志将 UIImage 存储在核心数据中 下一篇:iOS 5:如何将表情符号转换为 Unicode 字符?

相关文章