如何在 Swift 中在地图图钉周围添加圆圈? [英] How to add circle around map pin in Swift?

查看:27
本文介绍了如何在 Swift 中在地图图钉周围添加圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试解决这个问题,但没有运气.

我能做什么:

  • 显示用户的当前位置
  • 在我想要的任何地方显示图钉(取决于纬度和经度)

我想不通的地方:

如何在此位置周围创建地理围栏圈?

 func setupData() {//1. 检查系统是否可以监控区域如果 CLLocationManager.isMonitoringAvailable(对于:CLCircularRegion.self) {//2. 区域数据let title = "Marina Bar Hop"让坐标 = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)让 regionRadius = 300.0//3. 设置区域让区域 = CLCircularRegion(中心:CLLocationCoordinate2D(纬度:坐标.纬度,经度:坐标.经度),半径:regionRadius,标识符:标题)locationManager.startMonitoring(for: region)//4. 设置注解让餐厅注释 = MKPointAnnotation()restaurantAnnotation.coordinate = 坐标;restaurantAnnotation.title = "\(title)";mapView.addAnnotation(restaurantAnnotation)//5. 设置圆让 circle = MKCircle(中心:坐标,半径:regionRadius)mapView.addOverlay(圆圈)}别的 {print("系统无法跟踪区域")}}//6. 画圆func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->MKOverlayRenderer {让 circleRenderer = MKCircleRenderer(叠加:叠加)circleRenderer.strokeColor = UIColor.redcircleRenderer.lineWidth = 1.0返回 circleRenderer}

参见画圆"和设置圆"^

如果我没有看到错误,其余的代码是

I've been trying to figure this one out with no luck.

What I can do:

  • Show user's current location
  • Show a pin wherever I want (depending on lat and long)

What I can't figure out:

How do I create a geofenced circle around this location?

 func setupData() {
    // 1. check if system can monitor regions
    if CLLocationManager.isMonitoringAvailable(for: 
 CLCircularRegion.self) {

        // 2. region data
        let title = "Marina Bar Hop"
        let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
        let regionRadius = 300.0

        // 3. setup region
        let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,
            longitude: coordinate.longitude), radius: regionRadius, identifier: title)
        locationManager.startMonitoring(for: region)

        // 4. setup annotation
        let restaurantAnnotation = MKPointAnnotation()
        restaurantAnnotation.coordinate = coordinate;
        restaurantAnnotation.title = "\(title)";
        mapView.addAnnotation(restaurantAnnotation)

        // 5. setup circle
        let circle = MKCircle(center: coordinate, radius: regionRadius)
        mapView.addOverlay(circle)
    }
    else {
        print("System can't track regions")
    }
}

// 6. draw circle
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let circleRenderer = MKCircleRenderer(overlay: overlay)
    circleRenderer.strokeColor = UIColor.red
    circleRenderer.lineWidth = 1.0
    return circleRenderer
} 

See "draw circle" and "set up circle" ^

In case there's an error I'm not seeing, the rest of the code is here.

Any help is greatly appreciated! I thought I've done everything to solve this issue, but it still won't work. :( Thank you in advance!

解决方案

The trouble with this question is that the code in the question does not represent faithfully the real code. Fortunately, you also posted the real code:

https://github.com/kcapretta/RadiusSocialNetworkingApp/blob/master/RadiusMap/RadiusLocationViewController

In that version of the code, we see clearly that your delegate method

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay)
     -> MKOverlayRenderer {

is buried inside your viewDidLoad. Thus it is purely a local function, invisible to all other code. You need to get it out of there so that it is a method of the view controller.

To demonstrate, here's a slight variant on your code, leaving out the unnecessary stuff about the region monitoring and the user's current location and concentrating just on the annotation and the circle overlay:

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var mapView : MKMapView!
    let coordinate = CLLocationCoordinate2DMake(33.97823607957177, -118.43823725357653)
    // this is a _method_
    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.delegate = self

        mapView.region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 1000, longitudinalMeters: 1000)

        let title = "Marina Bar Hop"
        let restaurantAnnotation = MKPointAnnotation()
        restaurantAnnotation.coordinate = coordinate
        restaurantAnnotation.title = title
        mapView.addAnnotation(restaurantAnnotation)

        let regionRadius = 300.0
        let circle = MKCircle(center: coordinate, radius: regionRadius)
        mapView.addOverlay(circle)
    }
    // this is a _method_
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red
        circleRenderer.lineWidth = 1.0
        return circleRenderer
    }
}


The result:

这篇关于如何在 Swift 中在地图图钉周围添加圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆