Corelocation is a framework that provides device positioning in iOS. This framework allows for location processing to obtain positional data, such as longitude, latitude, elevation information, and so on.
1, positioning accuracy Settings Location Service Management class Cllocationmanager Desiredaccuracy attribute represents precision, there are 6 options:
Kcllocationaccuracybestfornavigation: Highest precision, generally used for navigation
Kcllocationaccuracybest: Best Accuracy
Kcllocationaccuracynearesttenmeters: Within 10m accuracy
Kcllocationaccuracyhundredmeters: Within 100m accuracy
Kcllocationaccuracykilometer: Within 1000m accuracy
Kcllocationaccuracythreekilometers: Within 3000m accuracy
2, Location Manager Update frequency settings We cannot directly control the frequency of Location manager updates, but can use the location Manager's Distancefilter property (in meters) for indirect control. It refers to how many meters the device (horizontal or vertical) moves before sending another update to the delegate. The higher the precision of the positioning requirement, the smaller the value of the Distancefilter property, the greater the application power consumption. 3, calculate the distance between two coordinates through the Distanceto method of the Cclocation object, we can get the distance between two coordinates, the unit is meters.
var currentlocation = cllocation (latitude:52.104526, longitude:51.111151) var targetlocation = CLLocation (Latitude: 52.105526, longitude:51.141151) var distance:cllocationdistance = Currentlocation.distancefromlocation ( targetlocation) println ("Distance between two points is: \ (distance)")
4, the following example shows how to obtain device-related location data (longitude, latitude, altitude, speed, etc.)
(1) Add a positional description in Info.plist (the value is empty): Nslocationwheninusedescription: Allows the description of GPs to be obtained at the foreground Nslocationalwaysusagedescription: Allows the description of GPs to be acquired in the background
(2) The code is as follows:
Import Uikitimport corelocation class Viewcontroller:uiviewcontroller, cllocationmanagerdelegate {//Location manager let Locationmanager:cllocationmanager = Cllocationmanager () @IBOutlet weak var label1:uilabel! @IBOutlet weak var label2:uilabel! @IBOutlet weak var label3:uilabel! @IBOutlet weak var label4:uilabel! @IBOutlet weak var label5:uilabel! @IBOutlet weak var label6:uilabel! @IBOutlet weak var label7:uilabel! Override Func Viewdidload () {super.viewdidload ()//Set Location Service Manager Agent Locationmanager.delegate = s Elf//Set positioning progress Locationmanager.desiredaccuracy = kcllocationaccuracybest//update distance Locationmanager. Distancefilter = 100////Send authorization Request locationmanager.requestalwaysauthorization () if (Cllocationmanager.lo Cationservicesenabled ()) {//allow use of location services, turn on Location Services Update locationmanager.startupdatinglocation () Print ("Position start")}}//PositioningChange execution, can get new position, old position func Locationmanager (Manager:cllocationmanager, didupdatelocations locations: [Cllocation]) { Get the latest coordinates let currlocation:cllocation = locations.last! Label1.Text = "Longitude: \ (currLocation.coordinate.longitude)"//Get latitude Label2.Text = "Latitude: \ (currlocation.coordinate.l Atitude) "//Get elevation Label3.text =" Elevation: \ (currlocation.altitude) "//get horizontal precision Label4.text =" Horizontal accuracy: \ (cu rrlocation.horizontalaccuracy) "//Get vertical precision Label5.text =" vertical accuracy: \ (currlocation.verticalaccuracy) "//Get direction Label6.text = "direction: \ (Currlocation.course)"//get speed Label7.text = "Speed: \ (currlocation.speed)"}}
Original from: www.hangge.com reprint retained original link: http://www.hangge.com/blog/cache/detail_783.html
Swift-use corelocation for positioning (latitude, longitude, altitude, speed, distance, etc.)