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

时间:2012-01-29 20:28:35

标签: ios ios5 mkmapview mkannotationview mkoverlay

(使用iOS 5和Xcode 4.2)

我有一个MKMapView,想在用户位置周围绘制一个1000米半径的圆圈。

从表面上看,似乎实现mapView:viewForAnnotation:地图视图委托方法,并为用户位置添加自定义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;
    }
}

但是,当您放大和缩小地图时,地图上的注释不会缩放。

所以我尝试添加叠加层(因为叠加层与地图重叠),使用MKCircle类并将其坐标设置为来自locationManger / map视图委托的最新坐标。但是由于MKCircle的coordinate property是只读的,我必须删除叠加层,然后每次用户移动时添加一个新叠加层。发生了明显的闪烁。

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

我非常感谢你的帮助:)。

7 个答案:

答案 0 :(得分:78)

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

MKCircle *circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:1000];
[map addOverlay:circle];

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

- (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;
}

答案 1 :(得分:44)

使用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
        }
    }
}

答案 2 :(得分:17)

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()
    }
}

然后这样打电话:

self.addRadiusCircle(location: CLLocation(latitude: YOUR_LAT_HERE, longitude: YOUR_LNG_HERE))

答案 3 :(得分:3)

尝试使用Apple Breadcrumb example

中的代码

答案 4 :(得分:2)

我不理解benwad的回答。 So here is clearer answer

添加圆圈非常容易。符合MKMapViewDelegate

@interface MyViewController : UIViewController <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@end

在viewDidLoad中,创建一个圆形注释并将其添加到地图中:

CLLocationCoordinate2D center = {39.0, -74.00};

// Add an overlay
MKCircle *circle = [MKCircle circleWithCenterCoordinate:center radius:150000];
[self.mapView addOverlay:circle];

然后实现mapView:viewForOverlay:返回视图。

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
{
    MKCircleView *circleView = [[MKCircleView alloc] initWithOverlay:overlay];
    [circleView setFillColor:[UIColor redColor]];
    [circleView setStrokeColor:[UIColor blackColor]];
    [circleView setAlpha:0.5f];
    return circleView;
}

但如果您希望圆圈始终具有相同的尺寸,无论缩放级别如何,您都必须做一些与众不同的事情。就像你说的那样,在regionDidChange:animated:中,获取latitudeDelta,然后创建一个新圆(半径适合宽度),删除旧圆并添加新圆。

我请注意:不要忘记将mapview与您的视图控制器委托连接起来。否则,不会调用viewForOverlay。

答案 5 :(得分:0)

添加圆圈很容易。符合MKMapViewDelegate。 按照下面的步骤,,,

第1步:

 CLLocationCoordinate2D center= {self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude};
// Add an overlay
MKCircle *circle= [MKCircle circleWithCenterCoordinate:center radius: 20000];//your distance like 20000(like meters)
[myMapView addOverlay:circle];

第2步:

 - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay
 {
    MKCircleView *C_View = [[MKCircleView alloc] initWithOverlay:overlay];
    [C_View setFillColor:[UIColor lightGrayColor]];
    [C_View setStrokeColor:[UIColor blackColor]];
    [C_View setAlpha:0.5f];

    return C_View;
 }

答案 6 :(得分:-1)

我要做的是,最后在地图套件上显示了称为以下功能的位置。

@IBOutlet weak var mapView: GMSMapView!

var cirlce: GMSCircle!

override func viewDidLoad() {

    super.viewDidLoad()
    mapView.delegate = self
    circleview(redius: 5000) 

  }

//used this func to draw the circle

 func circleview(redius:Double) {

    let  circleCenter = CLLocationCoordinate2D(latitude: 13.3450223, longitude: 74.7512519)

    cirlce = GMSCircle(position: circleCenter, radius: redius)
    cirlce.fillColor = UIColor(red: 230.0/255.0, green: 230.0/255.0, blue: 250.0/255.0, alpha:1.0)
    cirlce.strokeColor = .blue
    cirlce.strokeWidth = 2
    cirlce.map = mapView
  }