在MKMapView中围绕用户位置绘制一个1000米半径的圆

Jon*_*Cox 58 mkmapview mkannotationview ios mkoverlay ios5

(使用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,我不得不删除叠加层,然后每次用户移动时添加一个新叠加层.当它发生时引起明显的闪烁.

当地图视图缩小和缩小时,有没有办法无缝地制作注释比例?或者是否有一种很好的方法可以使叠加层随着用户位置的变化无缝移动?

我非常感谢你的帮助:)

ben*_*wad 79

尝试自定义叠加层.在viewDidLoad中添加:

MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];
Run Code Online (Sandbox Code Playgroud)

userLocation可以通过将MKUserLocationAnnotation存储为属性来获取.然后,要实际绘制圆圈,将其放在地图视图的委托中:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,iOS 7中不推荐使用`MKOverlayView`和`MKCircleView`(@ Hyperbole的观点).使用[`MKOVerlayRenderer`](https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKOverlayRenderer_class/Reference/Reference.html)和[`MKCircleRenderer`](https://developer.apple. com/library/ios/documentation/MapKit/Reference/MKCircleRenderer_class/Reference/Reference.html#// apple_ref/occ/cl/MKCircleRenderer). (18认同)

vla*_*iov 46

使用Swift的iOS 8.0更新版本.

import Foundation
import MapKit

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
    var locationManager: CLLocationManager = CLLocationManager()

    @IBOutlet var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        // We use a predefined location
        var location = CLLocation(latitude: 46.7667 as CLLocationDegrees, longitude: 23.58 as CLLocationDegrees)

        addRadiusCircle(location)
    }

    func addRadiusCircle(location: CLLocation){
        self.mapView.delegate = self
        var circle = MKCircle(centerCoordinate: location.coordinate, radius: 10000 as CLLocationDistance)
        self.mapView.addOverlay(circle)
    }

    func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
        if overlay is MKCircle {
            var circle = MKCircleRenderer(overlay: overlay)
            circle.strokeColor = UIColor.redColor()
            circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
            circle.lineWidth = 1
            return circle
        } else {
            return nil
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 无法在`mapView:rendererfForOverlay`中返回`nil`,请参阅修复:http://stackoverflow.com/a/32452494/723980 (4认同)

the*_*nde 19

Swift 3/Xcode 8在这里:

func addRadiusCircle(location: CLLocation){
    if let poll = self.selectedPoll {
        self.mapView.delegate = self
        let circle = MKCircle(center: location.coordinate, radius: 10)
        self.mapView.add(circle)
    }
}

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if overlay is MKCircle {
        let circle = MKCircleRenderer(overlay: overlay)
        circle.strokeColor = UIColor.red
        circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
        circle.lineWidth = 1
        return circle
    } else {
        return MKPolylineRenderer()
    }
}
Run Code Online (Sandbox Code Playgroud)

然后这样打电话:

self.addRadiusCircle(location: CLLocation(latitude: YOUR_LAT_HERE, longitude: YOUR_LNG_HERE))
Run Code Online (Sandbox Code Playgroud)

  • 什么是“self.selectedPoll”?它来自哪里 (2认同)