我在Swift中使用MapKit时出现问题。我希望它以特定坐标为中心,但默认中心根本不会改变。我viewController.swift文件中的第一(和最重要的部分)如下:MapKit不位于正确的位置
import UIKit
import Parse
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var map : MKMapView! = MKMapView()
var locManager:CLLocationManager!
@IBOutlet var txtUsername: UITextField!
@IBOutlet var txtPassword: UITextField!
override func viewDidLoad() {
var centerCoordinate = CLLocationCoordinate2DMake(41.8796677, -87.6198131)
var mapSpan = MKCoordinateSpanMake(0.01, 0.01)
var mapRegion = MKCoordinateRegionMake(centerCoordinate, mapSpan)
self.map.setRegion(mapRegion, animated: true)
locManager = CLLocationManager()
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestAlwaysAuthorization()
map.showsUserLocation = true
super.viewDidLoad()
}
可能是什么原因,该地图的中心不是由变量“centerCoordinate”改变了吗?
为什么你的代码在viewDidLoad之前?你也可以使用mapview – Katz
的图片更新问题,你应该在ViewDidAppear()中调用你的代码,因为你会知道你有用户位置 – Lamar