In an iOS system, you can use Corelocation to get to the user's current location, as well as device movement information.
Basic steps:
- Import Corelocation,
- Viewcontroller inherits the Cllocationmanagerdelegate protocol,
- The realization of Cllocationmanager didupdatelocations, Didupdatetolocation and other methods,
- Start positioning: Call the Cllocationmanager startupdatinglocation method.
- The device itself is positioned to be turned on.
Viewcontroller
ImportUIKitImportCorelocationclass Viewcontroller:uiviewcontroller, cllocationmanagerdelegate {@IBOutlet weakvarlatitudelabel:uilabel! @IBOutlet weakvarlongitudelabel:uilabel! Let Locationmanager:cllocationmanager = Cllocationmanager ()varcurrentlocation:cllocation! OverridefuncViewdidload () {super.viewdidload ()additional setup after loading the view, typically from a nib.Locationmanager.delegate = Self Locationmanager.desiredaccuracy = kcllocationaccuracybest LocationManager.di Stancefilter = Kcllocationaccuracykilometer} overridefuncViewwillappear (Animated:bool) {locationmanager.startupdatinglocation ()println("Start Location")} overridefuncViewwilldisappear (Animated:bool) {locationmanager.stopupdatinglocation ()println("Stop Location")} overridefuncDidreceivememorywarning () {super.didreceivememorywarning ()//Dispose of any resources, can be recreated.}funcLocationmanager (manager:cllocationmanager!, didupdatelocations locations: [anyobject]!) {println("Didupdatelocations") Currentlocation = Locations.last as! Cllocation Latitudelabel.text ="\ (currentLocation.coordinate.latitude)"Longitudelabel.text ="\ (currentLocation.coordinate.longitude)"}funcLocationmanager (manager:cllocationmanager!, didupdatetolocation newlocation:cllocation!, fromLocation oldLocation: cllocation!) {println("Didupdatetolocation") }funcLocationmanager (manager:cllocationmanager!, Didfailwitherror error:nserror!) {println("Didfailwitherror") }}
Simulator positioning settings
When using iphone simulator, you initially only see the print "Start location", which means that didupdatelocations was not successfully called.
The reason is that simulator by default will be positioned to close, we need to open it ourselves.
1. Open the Location debugging option, where we can choose Apple:
| 2. Set the targeting option in Settings->privacy->location in simulator, set to always: |
3. Go back to the app interface to see the location information: |
|
|
| 4. We can also choose our own simulation location, such as Hong Kong: |
5. You can see the latitude and longitude information corresponding to the analog location: |
| - |
- |
|
|
6. We can also set the location by entering the latitude and longitude values yourself:
The basic use of corelocation is the above, more complex later supplemented.
Corelocation---location information in iOS