import UIKit
import MapKit
import CoreLocation
import Foundation
import Darwin
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var annotation = MKPointAnnotation()
func getLocation()-> CLLocation! {
locationManager.startUpdatingLocation()
var x = locationManager.location
println(x.coordinate.latitude)
println(x.coordinate.longitude)
return x
}
我相信這個函數getLocation()是我的代碼中的問題。當前位置問題
//Other variables and other actions have been set up here that are irrelevant to the problem within the code.
var wS:CLLocationCoordinate2D! = nil
var carParked:Bool = false
var currentCoord:CLLocation!
override func viewDidLoad() {
//I have set up map, annotation, etc. in viewDidLoad and have tested that they all work perfectly fine.
}
@IBAction func pinButtonPressed(sender: AnyObject) {
carParked = true
var currentCoord = getLocation()
wS = currentCoord.coordinate
annotation.coordinate = wS
map.addAnnotation(annotation)
}
@IBAction func carFound(sender: AnyObject) {
carParked = false
map.removeAnnotation(annotation)
wS = nil
}
其中這兩個按鈕,pinButtonPressed或carFound將是錯誤的第二個最有可能的地方。
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
我想要什麼的代碼做的是,每當按下pinButtonPressed被標註將被放置在用戶的當前位置和呆在那裏。爲了刪除註釋,可以按carFound按鈕。然後,在第一個註釋放置之後按下carFound按鈕之後,應該可以再次按下pinButtonPressed,並且註釋應放置在用戶所在的位置,即使它們已移動。這個問題是pinButtonPressed第一次被按下,註釋調用getLocation()函數,找到當前位置,並將其設置爲註釋座標並放置註釋,然後使用carFound移除註釋;然後,我移動當前位置並再次按下pinButtonPressed函數,並將註釋放置在原來的時間位置,而不是移動到的位置。我已經放置了兩條打印線來打印getLocation()函數返回的座標,並且這些座標始終與自模擬開始運行後檢索到的第一個座標相同,無論我將當前位置移動到哪裏並按下pinButtonPressed按鈕。我相信我忽略了一些顯而易見的東西,並想知道是否有人有任何想法可以幫助。謝謝。