-1

我试图将 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, green: 0.98, 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
    }

}
4

1 回答 1

1

这个表达式:

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

就像说3 + Int

您需要提供类型 CLLocationCoordinate2D

您将类型本身作为参数提供给 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)
}
于 2021-05-26T09:36:35.470 回答