私はSwift 3のXcodeでマップの例を試しています。画面上にマップビューを配置し、プロジェクトをシミュレータで実行することができました。シミュレートされた場所を予想通りに変更すると、座標が移動するのがわかります。locationmanagerは座標を更新しません
現在の座標は印刷できません。私はクリックしたときに、locationmanagerによって与えられた座標がXcodeターミナルに表示されるように、画面上にボタンを置いた。私は以下のコードを含んでいます、showCoordinatesのボタンアクションは現在の座標を印刷することになっています。
import UIKit
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
@IBOutlet weak var myLattitude: UILabel! // These are the labels that will show the coordinate on the screen when pressed.
@IBOutlet weak var myLongitude: UILabel!
@IBAction func showCoordinates(_ sender: AnyObject) {
let myCoordinate = locationManager.location?.coordinate
print(myCoordinate) // This does not work as expected
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
guard status == .authorizedWhenInUse else {
print("Location not enabled")
return
}
print("Location allowed.")
mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Iシミュレータでこれを実行し、座標シミュレートを変更すると、マップ内の青い点は最初の位置を微移動しかしshowCoordinateボタンプリント。場所が変わっても、最初の場所が印刷されます。その機能を正しく機能させるにはどうすればいいですか?このように、直接あなたのdidUpdateLocations
コールバックで
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
showCoordinates(self)
}
また、あなたが入力でき、それ::
'ocationManager(_ manager:didUpdateLocations:)'が呼び出されていますか? – Larme
ありがとうございます。いいえ、私はそれを加えなかった。どこに置くことができますか? – Graeme