微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 MKCircle 添加到地图上的注释 - 设置 CLLocationCoordinate2D 的问题

如何解决将 MKCircle 添加到地图上的注释 - 设置 CLLocationCoordinate2D 的问题

我正在尝试将 MKCircle 添加到地图注释中。稍后它将用于检测该区域的用户并签入(如果您有任何提示如何稍后开始,我将不胜感激)。现在这是我的代码,但我收到一个错误

Cannot convert value of type 'CLLocationCoordinate2D.Type' to expected argument type 'CLLocationCoordinate2D'

这是我的代码,我还需要添加其他东西吗?:

import UIKit
import MapKit

class ViewController: UIViewController,MKMapViewDelegate{

    let locationManager = CLLocationManager()
    
    struct Szczyt {
      var name: String
      var describtion: String
      var lattitude: CLLocationdegrees
      var longtitude: CLLocationdegrees
    }
    
    @IBOutlet weak var mapView: MKMapView!
    @IBAction func mapTypeSegmentSelected(_ sender: UISegmentedControl) {
            switch sender.selectedSegmentIndex {
            case 0:
                mapView.mapType = .standard
            case 1:
                mapView.mapType = .satellite
            default:
                mapView.mapType = .hybrid
            }
        }
  **let circle = MKCircle(center: CLLocationCoordinate2D,radius: 100)** //Cannot convert value of type 'CLLocationCoordinate2D.Type' to expected argument type 'CLLocationCoordinate2D'

    let szczyty = [Szczyt(name: "one",describtion: "describtion one",lattitude: 50.333061725039226,longtitude: 16.708595782487315),Szczyt(name: "Two",describtion: "Describtion two",lattitude: 50.444874478583854,longtitude: 20.896341184611302),Szczyt(name: "Three",describtion: "Describiton three",lattitude: 50.555134079897516,longtitude: 15.884675411850157)]
    
 
    
    override func viewDidLoad() {
        super.viewDidLoad()
        checkLocationServices()
        findSthOnTheMap(szczyty)
        mapView.delegate = self
        
        }
        func checkLocationServices() {
          if CLLocationManager.locationServicesEnabled() {
            checkLocationAuthorization()
          } else {
          }
        }
        func checkLocationAuthorization() {
          switch CLLocationManager.authorizationStatus() {
          case .authorizedWhenInUse:
            mapView.showsUserLocation = true
           case .denied: 
           break
          case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            mapView.showsUserLocation = true
          case .restricted: 
           break
          case .authorizedAlways:
           break

          }
    }
    func findSthOnTheMap(_ szczyty: [Szczyt]) {
      for szczyt in szczyty {
        let annotations = MKPointAnnotation()
        annotations.title = szczyt.name
        annotations.subtitle = szczyt.opis
        annotations.coordinate = CLLocationCoordinate2D(latitude:
          szczyt.lattitude,longitude: szczyt.longtitude)
        mapView.addAnnotation(annotations)
        mapView.addOverlay(circle)
      }
    }
    
    func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard !(annotation is MKUserLocation) else { return nil }
        let annotationView = MKMarkerAnnotationView(annotation: annotation,reuseIdentifier: "MyMarker")
        switch annotation.title!! {
            case "one":
                annotationView.markerTintColor = UIColor(red: 0.86,green: 0.99,blue: 0.79,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            case "two":
                annotationView.markerTintColor = UIColor(red: 0.80,green: 0.98,blue: 0.73,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            case "three":
                annotationView.markerTintColor = UIColor(red: 0.73,blue: 0.68,alpha: 1.00)
                annotationView.glyphImage = UIImage(named: "example")
            default:
                annotationView.markerTintColor = UIColor.green
                annotationView.glyphImage = UIImage(named: "example")
        }
        return annotationView
    }
func mapView(_ mapView: MKMapView,rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        let circleRenderer = MKCircleRenderer(overlay: overlay)
        circleRenderer.strokeColor = UIColor.red
        circleRenderer.linewidth = 1.0
        return circleRenderer
    }

}

解决方法

这个表达:

let circle = MKCircle(center: CLLocationCoordinate2D,radius: 100)

就像在说3 + Int

您需要提供 type CLLocationCoordinate2D

value

您提供了类型本身作为 MKCircle 中心参数的参数

创建一个类似 CLLocationCoordinate2D(latitude: 0.0,longitude: 0.0) 的实例,以提供您希望圆居中的实际坐标

更新:如果您想为所有这些模型对象添加循环注释,那么您可能需要这样的内容:

注意一些错别字:

struct Szczyt {
    let name: String
    let describtion: String   // description
    let lattitude: CLLocationDegrees // latitude
    let longtitude: CLLocationDegrees // longitude
}

在我们模型的扩展中添加一个计算属性,让我们轻松获得它的 CLLocationCoordinate2D

extension Szczyt {
    var coordinate: CLLocationCoordinate2D {
        .init(latitude: lattitude,longitude: longtitude)
    }
}

let szczyty = [Szczyt(name: "one",describtion: "describtion one",lattitude: 50.333061725039226,longtitude: 16.708595782487315),Szczyt(name: "Two",describtion: "Describtion two",lattitude: 50.444874478583854,longtitude: 20.896341184611302),Szczyt(name: "Three",describtion: "Describiton three",lattitude: 50.555134079897516,longtitude: 15.884675411850157)]

从所有模型对象中创建一个 MKCircle 对象数组

let circles = szczyty.map {
    MKCircle(center: $0.coordinate,radius: 100)
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。