With the rise of location-based service lbs and mobile Internet, your location is an increasingly important information, location services are now popular applications such as the killer of social applications such as Mo mo. And in iOS development, Apple has provided us with a location interface, corelocation, we can use this interface convenient to obtain the latitude and longitude information of the current location. The specific implementation is as follows:
(1) Create a new Swift-based iOS project and import the Corelocation interface in Viewcontroller:
Import Corelocation
(2) The following are implemented in the Viewcontroller class:
Import Uikitimport corelocationclass viewcontroller:uiviewcontroller,cllocationmanagerdelegate {let LocationManager : Cllocationmanager = Cllocationmanager () override Func Viewdidload () {super.viewdidload () Locationma Nager.delegate = Self Locationmanager.desiredaccuracy = kcllocationaccuracybest If iOS8 () { Locationmanager.requestalwaysauthorization ()} locationmanager.startupdatinglocation () } func iOS8 ()->bool{return Uidevice.currentdevice (). systemversion = = "8.0"} func Locationmanager (manager:cllocationmanager!, didupdatelocations locations: [anyobject]!) {var location:cllocation = locations[locations.count-1] as! Cllocation if (location.horizontalaccuracy > 0) {println ("Latitude =\ (location.coordinate.latitude) , Longitude =\ (location.coordinate.longitude) ") Locationmanager.stopupdatinglocatioN ()}} func Locationmanager (manager:cllocationmanager!, Didfailwitherror error:nserror!) {println (Error)} override func didreceivememorywarning () {super.didreceivememorywarning () Dispose of any resources the can be recreated. }}
(3) Because the location information is more privacy information, to access the user's location information to ask the user's consent, so need to be prompted before running: Configure the content in Info.plist:
Key-value:nslocationusagedescription "program to access your location information"
Key-value:nslocationalwaysusagedescription "program to access your location information"
(4) Run the program to see the results:
。
。
Summing up, for the program output, and I am currently in the city's location information comparison, found that there is a large error, I do not know this because of what reason, at present I in a southern city, latitude information is in the north, may be Apple location Service has a bug it. At present, based on Baidu Map API, gold maps and other development applications are more, before I also use Baidu map Android SDK developed applications, interface is very convenient, positioning and other services are more comprehensive, very suitable for development, personally think if you want to carry out location services, or do not use the apple comes with it ...
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS project development Combat--use corelocation to get current location information