SWIFT – 在MKMapView中显示当前位置和更新位置

我正在学习如何使用新的Swift语言(只有SWIFT,没有Objective-C)。 要做到这一点,我想用一个地图(MKMapView)做一个简单的视图。 我想find并更新用户的位置(如在苹果地图应用程序中)。

我尝试过,但没有发生…

import MapKit import CoreLocation class MapView : UIViewController, CLLocationManagerDelegate { @IBOutlet weak var map: MKMapView! var locationManager: CLLocationManager! override func viewDidLoad() { super.viewDidLoad() if (CLLocationManager.locationServicesEnabled()) { locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() } } } 

你可以帮我吗 ?

您必须重写CLLocationManager.didUpdateLocationsCLLocationManager.didUpdateLocations一部分)以在位置pipe理器检索当前位置时得到通知:

 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { let location = locations.last as CLLocation let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) self.map.setRegion(region, animated: true) } 

注意:如果您的目标是iOS 8或更高版本,则必须在Info.plist中包含NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription项,以使位置服务正常工作。

对于swift 3和XCode 8,我find了这个答案:

  • 首先,您需要将隐私设置为info.plist。 插入stringNSLocationWhenInUseUsageDescription与您的描述为什么你想获得用户的位置。 例如,设置string“For map in application”。

  • 其次,使用这个代码示例

     @IBOutlet weak var mapView: MKMapView! private var locationManager: CLLocationManager! private var currentLocation: CLLocation? override func viewDidLoad() { super.viewDidLoad() mapView.delegate = self locationManager = CLLocationManager() locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest // Check for Location Services if CLLocationManager.locationServicesEnabled() { locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() } } // MARK - CLLocationManagerDelegate func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { defer { currentLocation = locations.last } if currentLocation == nil { // Zoom to user location if let userLocation = locations.last { let viewRegion = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 2000, 2000) mapView.setRegion(viewRegion, animated: false) } } } 
  • 第三,在storyboard中为mapView设置用户位置标志。

MyLocation是一个Swift iOS演示。

你可以使用这个演示如下:

  1. 显示当前位置。

  2. select其他位置:在这种情况下停止跟踪位置。

  3. 触摸时向MKMapView(iOS)添加一个推针。

对于Swift 2,你应该将其更改为以下内容:

 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let location = locations.last let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)) self.map.setRegion(region, animated: true) } 

你必须重写CLLocationManager.didUpdateLocations

  func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { let userLocation:CLLocation = locations[0] as CLLocation locationManager.stopUpdatingLocation() let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude) let span = MKCoordinateSpanMake(0.5, 0.5) let region = MKCoordinateRegion (center: location,span: span) mapView.setRegion(region, animated: true) } 

你还必须添加NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription到你的plist设置Result作为值

在Swift 4中,我使用了上面定义的locationManager委托函数。

 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 

..但这需要改变..

 func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

这来自.. https://github.com/lotfyahmed/MyLocation/blob/master/MyLocation/ViewController.swift – 谢谢!