Ios Swift-map返回起点

Ios Swift-map返回起点,ios,swift,maps,Ios,Swift,Maps,我的应用程序中有一张地图,但当我移动地图时,当我松开地图的那一刻,它就会回到起点。我不想发生这种事 我的ViewController看起来像这样的atm: import UIKit import MapKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var map: MKMapView! var locationManager:

我的应用程序中有一张地图,但当我移动地图时,当我松开地图的那一刻,它就会回到起点。我不想发生这种事

我的ViewController看起来像这样的atm:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    if (CLLocationManager.locationServicesEnabled())
    {
        locationManager = CLLocationManager()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }
}

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)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}
CLLocationManager将用户位置的更新发送到locationManager:didUpdateLocations:

调用此方法会导致位置管理器获得初始位置修复,这可能需要几秒钟,并通过调用其locationManager:didUpdateLocations:method通知您的代理


因此,在locationManager:didUpdateLocations:中,使用setRegion:animated:将地图移动到的区域是刚刚发送的位置,即用户的位置。

您可以在didUpdateLocations委托方法中设置地图视图的区域

一旦您开始调用startUpdatingLocation,它将开始不断检索您的位置坐标,并且将调用didUpdateLocations方法,并且您的地图视图将由方法中的setRegion更新


您可以将location manager的distanceFilter的值更改为更大的数字,这样更新的频率就会更低。默认情况下,使用kCLDistanceFilterNone,因此所有移动都将调用您的didUpdateLocations。

因此,似乎我只需将locationManager.StopUpdateLocation添加到didUpdateLocations函数中即可

这对我有点帮助,但我希望在用户启动应用程序时获得用户的当前位置,但当你停止更新它时,这是怎么做到的?你的问题和你原来的问题完全不同,你应该发表另一篇文章。如果您询问如何在获取用户的当前位置后停止更新mapView,我将告诉您使用StopUpdateLocation,而不是告诉您执行其他操作。