2015-06-23 81 views
1

我試圖抓住用戶位置的經度和緯度。我有一個代碼在棄用之前工作,我無法弄清楚如何解決它。座標棄用截至iOS 7

錯誤位於latestLocation.coordinate.latitude和latestLocation.coordinate.longitude的座標部分,錯誤狀態:'coordinate'不可用:從iOS 7及更早版本開始棄用的API在Swift中不可用。

謝謝你的幫助。

func locationManager(manager: CLLocationManager!, 
    didUpdateLocations locations: [AnyObject]!) 
{ 
    var latestLocation: AnyObject = locations[locations.count - 1] 

    latitude.text = String(format: "%.4f", 
     latestLocation.coordinate.latitude) 
    longitude.text = String(format: "%.4f", 
     latestLocation.coordinate.longitude) 



    if startLocation == nil { 
     startLocation = latestLocation as! CLLocation 
    } 


} 
+0

',不贊成使用的iOS 7的API和更早版本中Swift.'似乎無法自我解釋,不是嗎?谷歌搜索'IOS 8 SWIFT獲取用戶位置'似乎產生了良好的效果 –

回答

2

替換代碼

var latestLocation: AnyObject = locations[locations.count - 1] 

用下面的下面。

var latestLocation = locations.last as! CLLocation 
+0

謝謝瓦卡斯。非常感激。 –

+0

很高興爲您效勞。 – Vakas