62

(使用 iOS 5 和 Xcode 4.2)

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

从表面上看,实现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/地图视图委托的最新坐标。然而,由于 MKCircle 的坐标属性是只读的,我必须删除覆盖,然后在每次用户移动时添加一个新的。发生时会引起明显的闪烁。

当地图视图被缩放时,有什么方法可以无缝地使注释缩放?或者有没有一种好方法可以使覆盖层随着用户位置的变化而无缝移动?

我将非常感谢您的帮助:)

4

8 回答 8

80

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

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

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;
}
于 2012-02-10T11:55:46.637 回答
48

使用 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
        }
    }
}
于 2014-09-25T12:58:07.490 回答
25

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))
于 2016-12-14T02:35:55.010 回答
3

尝试使用Apple Breadcrumb 示例中的代码

于 2012-01-29T21:01:15.797 回答
2

我不明白 benwad 的答案。所以这里有更清晰的答案

添加一个圆圈非常容易。符合 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。

于 2014-12-21T17:19:32.720 回答
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
  }
于 2018-10-10T12:55:23.350 回答
0
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay

自 iOS 4.0 起已弃用

于 2015-09-14T10:55:54.207 回答
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;
 }
于 2015-09-02T06:12:47.030 回答