我已经试了一整天了——看起来应该很简单——但我无法让MapView放大当前位置。
在模拟器中使用"苹果"定位只是为了测试——我没有得到任何错误,它运行了——但视图只是停留在美国的全貌上,带有一个蓝点——在加利福尼亚州代表苹果的位置。如有任何帮助,请提前表示感谢!
代码如下:
import MapKit
import UIKit
import CoreLocation
class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
func createLocationManager () {
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
}
func locationManager (manager:CLLocationManager!, didUpdateLocations locations:[AnyObject]!) {
if let firstlocation = locations.first as? CLLocation {
mapView.setCenterCoordinate(firstlocation.coordinate, animated: true)
let region = MKCoordinateSpanMake (1000,1000)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager = CLLocationManager()
locationManager?.requestWhenInUseAuthorization()
self.mapView.showsUserLocation = true
}
在模拟器中尝试Debug->Location->City Bicycle Ride
。这将为您提供持续更新。
还要确保您正在设置locationManager的代理。