首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Swift中创建MKCircle?

如何在Swift中创建MKCircle?
EN

Stack Overflow用户
提问于 2015-10-23 00:38:46
回答 3查看 7.2K关注 0票数 9

Iv一直在四处寻找一个很好的解释,说明如何使用SWIFT2.0为MKCircle编写MapView注释,但我似乎找不到足够的解释。有人可以发布一些示例代码来展示如何创建MKCircle注释吗?这是我用来绘制地图和获取坐标的代码。

代码语言:javascript
复制
let address = self.location

let geocoder = CLGeocoder()

    geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
        if((error) != nil){
            print("Error", error)
        }
        if let placemark = placemarks?.first {
            let coordinates:CLLocationCoordinate2D = placemark.location!.coordinate

            self.locationCoordinates = coordinates
            let span = MKCoordinateSpanMake(0.005, 0.005)
            let region = MKCoordinateRegion(center: self.locationCoordinates, span: span)
            self.CIMap.setRegion(region, animated: true)

            let annotation = MKPointAnnotation()
            annotation.coordinate = self.locationCoordinates
            self.CIMap.addAnnotation(annotation)

            self.CIMap.layer.cornerRadius = 10.0

            self.CIMap.addOverlay(MKCircle(centerCoordinate: self.locationCoordinates, radius: 1000))
        }
    })
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-07-13 10:38:03

将展示如何使用xcode 8.3.3在地图视图上创建循环覆盖的步骤。

在您的主情节提要文件中,将mapView视图拖到情节提要的场景(视图)上,并为同样的场景创建出口,这里我创建了mapView。此外,无论何时在地图上按长键,您都希望动态地创建覆盖,因此将long press手势识别器从对象库拖到mapView上,然后为相同的操作方法创建操作方法,这里我为相同的对象创建了addRegion()。

  1. 为CLLocationManager类创建一个全局常量,以便可以在每个函数中访问它。在您的viewDidLoad方法中,添加一些代码以获得用户的授权。 导入UIKit导入MapKit类ViewController: UIViewController { @IBOutlet : MKMapView!设locationManager = CLLocationManager()覆盖func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestAlwaysAuthorization() locationManager.requestAlwaysAuthorization locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.startUpdatingLocation() }
  2. 在您的长按压手势识别器动作方法addRegion()中添加代码以生成循环区域。 @IBAction func addRegion(_ longPress : Any) { print("addregion按下“)保护让longPress=发件人作为?UILongPressGestureRecognizer center{ let } let touchLocation = longPress.location(in: mapView) let坐标= mapView.convert(touchLocation,toCoordinateFrom: mapView) let region =CLCircularRegion(中心:坐标,半径: 5000,标识符:"geofence") mapView.removeOverlays(mapView.overlays) locationManager.startMonitoring(for: mapView)让圆=MKCircle(中心:坐标,radius: region.radius) mapView.add(圆)}

不过,在地图上呈现圆圈之前,您不会在地图上实际看到圆圈。为此,您需要实现implement委托的委托。

代码语言:javascript
复制
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {}
  1. 为了使代码看起来更简洁,您可以在类结束的最后一个大括号之后创建扩展。一个扩展包含CLLocationManagerDelegate的代码,另一个包含MKMapViewDelegate的代码。 扩展ViewController: CLLocationManagerDelegate { func locationManager(_ locationManager: CLLocationManager,didUpdateLocations locations: CLLocation) { locationManager.stopUpdatingLocation() mapView.showsUserLocation = true }

您应该在委托方法中调用locationManager.stopUpdatingLocation(),这样电池就不会耗尽。

代码语言:javascript
复制
        extension ViewController: MKMapViewDelegate {
            func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
                guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}

                let circleRenderer = MKCircleRenderer(circle: circelOverLay)
                circleRenderer.strokeColor = .blue
                circleRenderer.fillColor = .blue
                circleRenderer.alpha = 0.2
                return circleRenderer
            }
        }

这里我们在屏幕上画一个实际的圆圈。

最后的代码应该如下所示。

代码语言:javascript
复制
import UIKit
import MapKit

class ViewController: UIViewController {


    @IBOutlet var mapView: MKMapView!
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.requestWhenInUseAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

    }

    // MARK: Long Press Gesture Recognizer Action Method

    @IBAction func addRegion(_ sender: Any) {
        print("addregion pressed")
        guard let longPress = sender as? UILongPressGestureRecognizer else {return}

        let touchLocation = longPress.location(in: mapView)
        let coordinates = mapView.convert(touchLocation, toCoordinateFrom: mapView)
        let region = CLCircularRegion(center: coordinates, radius: 5000, identifier: "geofence")
        mapView.removeOverlays(mapView.overlays)
        locationManager.startMonitoring(for: region)
        let circle = MKCircle(center: coordinates, radius: region.radius)
        mapView.add(circle)

    }

}

extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationManager.stopUpdatingLocation()
        mapView.showsUserLocation = true
    }
}

extension ViewController: MKMapViewDelegate {
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        guard let circelOverLay = overlay as? MKCircle else {return MKOverlayRenderer()}

        let circleRenderer = MKCircleRenderer(circle: circelOverLay)
        circleRenderer.strokeColor = .blue
        circleRenderer.fillColor = .blue
        circleRenderer.alpha = 0.2
        return circleRenderer
    }
}
票数 6
EN

Stack Overflow用户

发布于 2015-10-23 00:57:06

首先,您需要将MKMapViewDelegate添加到类防御中。

代码语言:javascript
复制
 mapView.delegate = self 

在viewDidLoad中将映射委托设置为self。

设置注释

代码语言:javascript
复制
mapView.addOverlay(MKCircle(centerCoordinate: CLLocationCoordinate2D, radius: CLLocationDistance))

现在应该在mapView委托中调用rendererForOverlay,并在那里绘制它。

代码语言:javascript
复制
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
    if let overlay = overlay as? MKCircle {
        let circleRenderer = MKCircleRenderer(circle: overlay)
        circleRenderer.fillColor = UIColor.blueColor()
        return circleRenderer
    }
}

此外,您还需要导入MapKit以使其全部编译

票数 14
EN

Stack Overflow用户

发布于 2015-10-23 00:48:06

重叠仅仅是一组数字。在地图视图中可见的东西是一个覆盖渲染器。您必须实现mapView:rendererForOverlay:来提供覆盖渲染器;否则,您将什么也看不到。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33293075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档