CoreLocation --- location information in iOS
In iOS, you can use CoreLocation to obtain the user's current location and device mobile information.
Basic Steps: import CoreLocation. ViewController inherits the CLLocationManagerDelegate Protocol to implement methods such as didUpdateLocations and didUpdateToLocation of CLLocationManager. Start locating: Call the startUpdatingLocation method of CLLocationManager. The device positioning must be enabled. ViewController
import UIKitimport CoreLocationclass ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet weak var latitudeLabel: UILabel! @IBOutlet weak var longitudeLabel: UILabel! let locationManager: CLLocationManager = CLLocationManager() var currentLocation: CLLocation! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. locationManager.delegate = self locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.distanceFilter = kCLLocationAccuracyKilometer } override func viewWillAppear(animated: Bool) { locationManager.startUpdatingLocation() println("start location") } override func viewWillDisappear(animated: Bool) { locationManager.stopUpdatingLocation() println("stop location") } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { println("didUpdateLocations") currentLocation = locations.last as! CLLocation latitudeLabel.text = "\(currentLocation.coordinate.latitude)" longitudeLabel.text = "\(currentLocation.coordinate.longitude)" } func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) { println("didUpdateToLocation") } func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) { println("didFailWithError") }}
Simulator positioning settings
When I used the iPhone simulator, I initially only saw the print "start location", that is, I did not successfully call didUpdateLocations.
The reason is that the simulator will be positioned and disabled by default, and needs to be enabled by ourselves.
1. Open the positioning debugging option. Here, we can select Apple:
2. Set Settings-> Privacy-> Location in simulator to always: |
3. Return to the APP interface to view the location information: |
|
|
4. You can also select the simulated location, for example, Hong Kong: |
5. You can see the latitude and longitude information of the corresponding simulated location: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vdgq + DQoJPC90cj4NCgk8dHI + samples + PGltZyBhbHQ9 "select a simulated location" src = "http://www.bkjia.com/uploads/allimg/150425/041I2K28-3.png" title = "\"/> |
|
6. You can also enter the longitude and latitude values to set the location:
The basic use of CoreLocation is the above, which will be added later in a more complex manner.