iOS location and positioning system--Getting Started notes

Source: Internet
Author: User

This is due to the location permission setting issue caused by the Xcode upgrade.
Upgrade Xcode6, Xcode7 after opening the previous XCODE5 project, the program cannot be located. Project upgrade to Xcode6 or Xcode7 compile requires iOS8 to write their own authorization, or do not have permission to locate.
Workaround:
First, the corresponding default field is added to the Info.plist, the value is set to Yes (foreground position write top field, front and back position write bottom field)
Nslocationwheninuseusagedescription//Allow GPS description to be obtained at the front desk
Nslocationalwaysusagedescription//Allow the description of GPs to be obtained in front and backstage

Diagram of the setting:

Well, if you set it up, go to coding learning, and familiarize yourself with the classes, methods, and properties that Apple provides about location services:

1, Location services and map application Location services: Get the user's current location information, the user's location information to do relevant data processing.

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

    • Location Services to master:
      • Main classes of operations: Cllocationmanager
      • Owning Library: corelocation
      • Structure: cllocationcoordinate2d (latitude and longitude), Clclocationcoorregion (region)
    • The map application needs to be mastered:
      • Frame: Mapkit
      • Operation class: Mkmapview

2. Location Services

  • Property:
    • Desiredaccuracy Set positioning accuracy, this is a constant property, generally with best
    • Distancefilter minimum change distance for repositioning
  • Method:

    • Set when to turn on the status of the location
      • Requestalwaysauthorization () always turn on positioning
      • Requestwheninuseauthorization () When the app enters the foreground, open the location (IOS8 's New method)
      • Class Method Locationservicesenabled () whether there is a Location service feature (Cllocationmanager)
      • Startupdatinglocation () Open position
  • Agent:

    • Agent's protocol:
    • Proxy method: You can directly into the library of the API view, as long as the location of the wrong call proxy method, positioning the successful invocation of the proxy method, and so on;
  • The objects involved

    • Locations:cllocation the properties of the Cllocation object:
      • Coordinate
        • Longitude/latitude
  • English Vocabulary accumulation:

    • Accuracy English ' Ækj?r?s? N. [number] accuracy, accuracy
    • Filter English ' F?lt? Filter filters; filter filter; infiltration; remove by filtration
    • //
      Viewcontroller.swift
      Locationmanager
      //
      Created by Heyang on 16/1/26.
      COPYRIGHT©2016 year Heyang. All rights reserved.
      //

      Import UIKit

      Need to import corelocation framework
      Import Corelocation

      Class Viewcontroller:uiviewcontroller,cllocationmanagerdelegate {

      Declaring a global variable
      var locationmanager:cllocationmanager!

      Override Func Viewdidload () {
      Super.viewdidload ()
      Locationmanager = Cllocationmanager ()

      Setting the accuracy of positioning
      Locationmanager.desiredaccuracy = Kcllocationaccuracybest

      Set the minimum distance distance filter for positioning changes
      Locationmanager.distancefilter = 50

      Set the status of the request location
      If #available (IOS 8.0, *) {
      Locationmanager.requestwheninuseauthorization ()
      } else {
      Fallback on earlier versions
      Print ("Hello")
      }//This is only after iOS8.

      Sets the proxy as the current object
      Locationmanager.delegate = self;

      If cllocationmanager.locationservicesenabled () {
      Turn On location Services
      Locationmanager.startupdatinglocation ()
      }else{
      Print ("No location Service")
      }

      }
      Proxy method for locating failed calls
      Func Locationmanager (Manager:cllocationmanager, Didfailwitherror error:nserror) {
      Print (Error)
      }
      Locating proxy methods for updating geographic information calls
      Func Locationmanager (Manager:cllocationmanager, didupdatelocations locations: [Cllocation]) {
      If Locations.count > 0
      {
      Let Locationinfo = locations.last!
      Let Alert:uialertview = Uialertview (title: "Acquired geographic coordinates",
      Message: "Longitude is: \ (locationInfo.coordinate.longitude), dimension is: \ (locationInfo.coordinate.latitude)",
      Delegate:nil, Cancelbuttontitle: "Yes")
      Alert.show ()
      }
      }
      }

    • //
      Viewcontroller.m
      Locationmanager
      //
      Created by Heyang on 16/1/26.
      COPYRIGHT©2016 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 = 50;

      Set usage status for location services
      [Locationmanager requestwheninuseauthorization];
      Locationmanager.delegate = self;

      if ([Cllocationmanager locationservicesenabled]) {
      [Locationmanager startupdatinglocation];
      }else{
      NSLog (@ "This machine does not support location services");
      }

      Self.locationmanager = Locationmanager;
      }
      Proxy method for locating failed calls
      -(void) Locationmanager: (Cllocationmanager *) Manager didfailwitherror: (Nserror *) error{
      NSLog (@ "error message:%@", error);
      }
      Proxy method for locating data update calls
      -(void) Locationmanager: (Cllocationmanager *) Manager didupdatelocations: (nsarray<cllocation *> *) locations{
      if (Locations.count > 0) {
      cllocation* location = Locations.lastobject;
      Cllocationcoordinate2d coordinate2d = location.coordinate;
      nsstring* message = [NSString stringwithformat:@ "Longitude:%lf, Dimension is:%lf", coordinate2d.longitude,coordinate2d.latitude];
      uialertview* Alertview = [[Uialertview alloc] initwithtitle:@ "displays the latitude and longitude of the current position"
      Message:message delegate:nil cancelbuttontitle:@ "Cancel" otherbuttontitles:@ "OK", nil];
      [Alertview show];

      }
      }

      @end

iOS location and positioning system--Getting Started notes

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.