iOS project development Combat--use corelocation to get current location information

Source: Internet
Author: User

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

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.