将 MKCircle 添加到地图上的注释 - 设置 CLLocationCoordinate2D 的问题
Adding MKCircle to the annotations on the map - Problem with setting 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, 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
}
}
这个表达式:
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: [=13=].coordinate, radius: 100)
}
我正在尝试将 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
}
}
这个表达式:
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: [=13=].coordinate, radius: 100)
}