簡體   English   中英

Swift:如何使用CLLocationCoordinate2D將坐標存儲為字符串或int?

[英]Swift: How can I store coordinates using CLLocationCoordinate2D as a string or an int?

到目前為止,這是我的代碼。 我正在嘗試將locValue.latitude和locValue.longitude的值存儲為字符串或整數。 我已經嘗試在方法中定義返回類型( - > String等),但是我得到一個錯誤,說明didChangeAuthorizationStatus方法與協議CLLocationManagerDelegate中的可選需求方法locationManager沖突。

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedAlways {
        let locValue:CLLocationCoordinate2D = manager.location!.coordinate
        self.dispLocation.text = "locations = \(locValue.latitude) \(locValue.longitude)"
    }
}

只需聲明兩個字符串

var lat  = ""
var long = ""

並且從您的位置解析latlong

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location:CLLocationCoordinate2D = manager.location!.coordinate
    lat = String(location.latitude)
    long = String(location.longitude)
}

你也可以嘗試這個 -

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    var locValue:CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0);

    if manager.location?.coordinate != nil {
        locValue = (manager.location?.coordinate)!
    }

    let locations = "locations = \(locValue.latitude) \(locValue.longitude)"
    print(locations)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM