无法将'CLLocationCoordinate2D'类型的值转换为预期的参数类型'CLLocationDegrees'(又名'Double')

时间:2017-12-06 07:01:06

标签: ios swift mapkit core-location

我已获取用户的当前位置,但在此行收到此错误

let region : MKCoordinateRegion = MKCoordinateSpanMake(myLocation, span)

我在上面的 myLocation

中收到错误
  

无法将'CLLocationCoordinate2D'类型的值转换为预期的参数类型'CLLocationDegrees'(又名'Double')

我认为可能存在类型转换问题或类似问题。

我应该怎么做来处理这个错误我研究了很多例子,但没有人帮助过我。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

let manager = CLLocationManager()

@IBOutlet weak var myMap: MKMapView!

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


    let location = locations[0]

    let span : MKCoordinateSpan = MKCoordinateSpanMake(1, 1)
    let myLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region : MKCoordinateRegion = MKCoordinateSpanMake(myLocation, span)



    myMap.setRegion(region, animated: true)

    self.myMap.showsUserLocation = true


}

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要MKCoordinateRegionMake,而不是MKCoordinateSpanMake

还有很多其他修复方法:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[0]

    let span = MKCoordinateSpanMake(0.01, 0.01)
    let myLocation = location.coordinate
    let region = MKCoordinateRegionMake(myLocation, span)

    myMap.setRegion(region, animated: true)

    self.myMap.showsUserLocation = true
}
相关问题