IOS Introductory Notes Location system _ios

Source: Internet
Author: User
Tags reserved

About geographical position and location system, in the development of iOS is also more common, such as the United States outside the restaurant shop search, it first needs the user's current mobile phone location, and then in this location near the location of the relevant food and beverage outlets, and provide relevant food and beverage information, and the most common is the map navigation, Map navigation requires positioning services, and then choose a route based on the user's destination. In fact, as a mobile phone user for such a long time, more or less will find in some app applications for the first time after your mobile phone installation success, the first boot may be prompted "whether to agree with xxx (such as Baidu Browser) to get the current location" and so on such information. Visible geographical location and positioning system is an essential skill for enterprise app development.

This chapter will provide a two-version entry code for the swift and Objective-c, respectively, to achieve the geographic latitude and longitude coordinates of the current phone or simulator.

Tips for writing before a formal study:

This is because of the positioning permission Setup problem caused by the Xcode upgrade.
Upgrade Xcode6, Xcode7 after opening before the XCODE5 project, the program can not locate. Engineering upgrade to Xcode6 or Xcode7 compile iOS8 need to write their own authorization, or do not have permission to locate.

Workaround:

First in the info.plist to add the corresponding default field, the value is set to Yes (foreground position to write the top field, front and rear position to write the bottom field)
Nslocationwheninuseusagedescription//Allow access to GPS description in the foreground
Nslocationalwaysusagedescription//Allow GPS description to be obtained in front and backstage

Diagram of settings:


Well, if you set it up, go into coding and learn, first of all, about the classes, methods, and attributes that Apple offers about locating services:

1, positioning services and map application of the introduction

Location Service: Get the user's current position information, and do relevant data processing for the user's position information.

Map application: According to the actual needs of the map and the surrounding environment information, based on the user's current location to show the user's attention to map location information, as well as navigation for users.

• Positioning services to master:

• Main Operation class: Cllocationmanager

• Owning library: corelocation

• Structure: cllocationcoordinate2d (latitude and longitude), Clclocationcoorregion (area)

• Map applications need to master:

• Frame: Mapkit

• Operation class: Mkmapview

2, positioning services

Property

desiredaccuracy set positioning accuracy, which is a constant property, generally with best
Minimum variation distance of Distancefilter relocation

Method:

• Set when to turn on the status requestalwaysauthorization () always open the location
requestwheninuseauthorization () Open positioning (iOS8 new method) when app enters the foreground
• Class Method Locationservicesenabled () whether there is a positioning service function (Cllocationmanager)
startupdatinglocation () Open positioning

Agent:

• Agent's agreement:
• Agent method: You can directly access the library's API view, as long as it is to locate the proxy method of error invocation, positioning the successful invocation of the proxy method, etc.;

The objects involved

Locations:cllocation The properties of the Cllocation object: coordinate longitude/latitude

English Vocabulary accumulation:

accuracy English ' ækjʊrəsɪn. [number] precision, accuracy
Filter English ' fɪltə filter filter; filter filter; infiltration; removal by filtration

The following is provided by Swift source code:

Viewcontroller.swift//Locationmanager////Created by Heyang on//. Copyright© year Heyang.
All rights reserved. Import Uikit//needs to be imported into the Corelocation framework import corelocation class Viewcontroller:uiviewcontroller,
Cllocationmanagerdelegate {//Declare a global variable var locationmanager:cllocationmanager! override func Viewdidload () { Super.viewdidload () Locationmanager = Cllocationmanager ()//set positioning accuracy Locationmanager.desiredaccuracy = Kcllocationaccuracybest//Set Position change minimum distance filter locationmanager.distancefilter =//Set request location status if #available (IOS., *) {Loca Tionmanager.requestwheninuseauthorization ()} else {//fallback on earlier versions print ("Hello")}//this is only available after iOS//set generation
The current object locationmanager.delegate = self; If cllocationmanager.locationservicesenabled () {//Open Locator Service locationmanager.startupdatinglocation ()}else{print (" No locator Service "}}//Location failed call Proxy method func Locationmanager (Manager:cllocationmanager, Didfailwitherror error:nserror) {print (Erro R)}//Locate the proxy method for updating geographic information calls Func Locationmanager (manAger:cllocationmanager, didupdatelocations locations: [Cllocation]) {if locations.count > {Let locationinfo = Locat
ions.last! Let Alert:uialertview = Uialertview (title: "Obtained geographical coordinates", message: "Longitude is: \ (locationInfo.coordinate.longitude), dimension is: \ ( LocationInfo.coordinate.latitude) ", Delegate:nil, Cancelbuttontitle:" Yes ") Alert.show ()}}

The following is the source code for OBJECTIVE-C:

VIEWCONTROLLER.M//Locationmanager////Created by Heyang on//. Copyright© year Heyang.
All rights reserved. #import "ViewController.h" #import <CoreLocation/CoreLocation.h> @interface Viewcontroller () <
Cllocationmanagerdelegate>/** Global Positioning object/@property (Nonatomic,strong) Cllocationmanager *locationmanager;
@end @implementation Viewcontroller-(void) viewdidload {[Super viewdidload];
cllocationmanager* Locationmanager = [[Cllocationmanager alloc] init];
Set positioning accuracy locationmanager.desiredaccuracy = kcllocationaccuracybest;
Set positioning change minimum distance locationmanager.distancefilter =; 
Set the usage status of the positioning service [Locationmanager requestwheninuseauthorization];
Locationmanager.delegate = self; if ([Cllocationmanager locationservicesenabled]) {[Locationmanager startupdatinglocation];}
else{NSLog (@ "Native does not support positioning service function");} Self.locationmanager = Locationmanager; //Locate the proxy method for the failed call-(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (nserror *) error{NSLog (@ "error message:
%@ ", error); }
//Locate the proxy method for the data Update call-(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *
locations{if (Locations.count >) {cllocation* location = Locations.lastobject;
cllocationcoordinated coordinated = location.coordinate;
nsstring* message = [NSString stringwithformat:@ "Longitude:%lf, Dimension is:%lf", coordinated.longitude,coordinated.latitude]; uialertview* Alertview = [[Uialertview alloc] initwithtitle:@ "show latitude and longitude of current position" Message:message Delegate:nil can
celbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];
[Alertview show];  }} @end

The above is a small compilation for you to share the iOS introductory notes of the geographical positioning system, I hope to help.

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.