我的应用程序中有一张地图但是当我移动地图时,第二个我松开,它返回到起点。我不希望这发生..
我的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.
}
}
答案 0 :(得分:0)
CLLocationManager
的{{3}}将用户所在位置的更新发送至locationManager:didUpdateLocations:
。
调用此方法会导致位置管理器获取初始位置修复(可能需要几秒钟),并通过调用其locationManager:didUpdateLocations:method来通知您的代理。
然后在locationManager:didUpdateLocations:
中,您将地图移动到setRegion:animated:
的区域是刚刚发送的位置,即用户的位置。
答案 1 :(得分:0)
您可以在didUpdateLocations
委托方法中设置mapView的区域。
一旦您开始致电startUpdatingLocation()
,它就会开始继续检索您的位置坐标,并且您的didUpdateLocations
方法将被调用,并且您的mapView将由setRegion()
更新你的方法。
您可以将位置管理器distanceFilter
的值更改为更大的数字,以便更新频率更低。默认情况下,会使用kCLDistanceFilterNone
,因此系统会调用didUpdateLocations
所有动作。
答案 2 :(得分:0)
所以我似乎只需要将locationManager.stopUpdatingLocation()
添加到didUpdateLocations
函数