【发布时间】:2015-02-09 21:47:53
【问题描述】:
我已经尝试了一整天 - 看起来应该很简单 - 但我无法让 MapView 放大当前位置。
在模拟器中使用“Apple”作为该位置仅用于测试 - 我没有收到任何错误并且它运行 - 但视图只是停留在带有蓝点的美国全貌 - 在加利福尼亚州代表 Apple地点。提前感谢任何帮助!
代码如下:
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
}
【问题讨论】:
-
你是在调用你的 createLocationManager() 方法吗?否则你没有设置委托。
-
Onato - 这听起来像是一个愚蠢的问题 - (它可能是)。我认为它会在 mapviewcontroller 执行时自动调用。 I have a tableviewcontroller - that when the row is selected it takes you to the map (and this mapviewcontroller.swift with the code above in it) that is supposed to show your current location (zoomed in of course) - how do I get the在此设置下调用的方法?
-
不,这里没有魔法。您正在 viewDidLoad 中重复其中的一些代码。只需将其替换为
createLocationManager()。
标签: ios swift mapkit core-location