I have a map in my app but when i move the map around, the second I let go, it returns to the starting point. I do not want this to happen..
My ViewController looks like this 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
's startUpdatingLocation
sends updates to the User's location to locationManager:didUpdateLocations:
.
Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager:didUpdateLocations: method.
So then in locationManager:didUpdateLocations:
the region you're moving the map to with setRegion:animated:
is the location that just got sent, ie, the User's location.
You set the region of your mapView in the didUpdateLocations
delegate method.
Once you start to call startUpdatingLocation()
, it will start to keep retrieving your location coordinate, and your didUpdateLocations
method will be called, and your mapView will be update by the setRegion()
in your method.
You can change the value of location manager's distanceFilter
to a bigger number, so it will update less frequently. By default, kCLDistanceFilterNone
is used, so your didUpdateLocations
will be called of all movements.
所以我似乎只需将locationManager.stopUpdatingLocation()
添加到didUpdateLocations
函数中
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.