【问题标题】:Cannot convert value of type 'CLLocationCoordinate2D' to expected argument type 'CLLocationDegrees' (aka 'Double')无法将“CLLocationCoordinate2D”类型的值转换为预期的参数类型“CLLocationDegrees”(又名“Double”)
【发布时间】:2017-12-06 07:01:05
【问题描述】:

我得到了用户的当前位置,但是在这一行得到了这个错误

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.
}


}

【问题讨论】:

    标签: ios swift mapkit core-location


    【解决方案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
    }
    

    【讨论】:

    • 这显示了旧金山的用户?如何获得当前状态?还是在模拟器上我无法获得当前状态?
    • 这是一个与您最初提出的问题完全不同的问题。我的回答解决了你原来的问题吗?如果是这样,请通过单击答案左侧的复选标记来表明。至于您的最新评论,请搜索一下。有很多关于在模拟器中设置位置的信息。
    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 2022-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多