CoreLocation --- location information in iOS

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.