IOS geolocation system

  • 2020-05-14 04:52:46
  • OfStack

Foreword: about location and positioning system, is more common in the iOS development, such as search Meituan outside catering shops, it requires the user to the current position of the mobile first, and then in this location near the search related catering shops location, and provide the relevant food information, such as the most common is to map navigation, map navigation need more location-based services, and then choose one route according to the user's destination. In fact, as a mobile phone user for such a long time, you will more or less find that after the first successful installation of some app applications on your phone, the first launch may prompt "do you agree to XXx (such as baidu browser) to get the current location" and other information. Visible location and location systems are essential skills for enterprise app development.

This chapter will provide entry codes for the Swift version and Objective-C version, respectively, to display the latitude and longitude coordinates of the current phone or emulator.

Tips written before formal study:

This is due to location permission Settings issues caused by the xcode upgrade.
After upgrading xcode6, xcode7, open the previous xcode5 project, the program cannot be located. When the project is upgraded to xcode6 or xcode7, iOS8 needs to write authorization by itself, otherwise there is no permission to locate.

Solutions:

First, add the corresponding default field in info.plist, and set the value to YES (foreground location writes the top field, front and background location writes the bottom field).
NSLocationWhenInUseUsageDescription // allows the description of GPS to be obtained in the foreground
NSLocationAlwaysUsageDescription // allows you to get descriptions of GPS in front and behind the scenes

Icon of setting:


Ok, if it is set up, then it is time to enter the coding study. First, familiarize yourself with the classes, methods and properties related to the location service provided by apple:

1. Introduction to location services and map applications

Location service: obtain the user's current location information, and do relevant data processing for the user's location information.

Map application: display the map and surrounding environment information according to the actual needs, display the map location information that users are concerned about based on the user's current location, and navigate for users.

The & # 8226; Location services to master:

The & # 8226; Class of primary operations: CLLocationManager

The & # 8226; Owned library: CoreLocation

The & # 8226; Structure: CLLocationCoordinate2D (latitude and longitude), CLCLocationCoorRegion (region)

The & # 8226; Map application needs to master:

The & # 8226; Framework: MapKit

The & # 8226; Operating class: MKMapView

2. Location service

The & # 8226; Properties:

The & # 8226; desiredAccuracy sets the positioning accuracy, which is a constant property, usually best
The & # 8226; Minimum change distance for distanceFilter repositioning

Methods:

The & # 8226; Set when to turn on the positioning state. requestAlwaysAuthorization() always open the location
The & # 8226; requestWhenInUseAuthorization() open location when app enters the foreground (new method for iOS8)
The & # 8226; Class method locationServicesEnabled() has location service capability (CLLocationManager)
The & # 8226; startUpdatingLocation() enables positioning

Agent:

The & # 8226; Agency agreement:
The & # 8226; Proxy method: you can directly enter the API of this library to view, as long as it is to locate the proxy method that was called incorrectly, locate the proxy method that was successfully called, etc.;

The objects involved

The & # 8226; Properties of the CLLocation object: • coordinate & # 8226; longitude/latitude

English vocabulary:

The & # 8226; accuracy British '& aelig; kj & # 650; r & # 601; s & # 618; n. [number] accuracy
The & # 8226; filter British 'f & # 618; lt & # 601; Filter filter; Screening; Light filter filtration; Permeability; Remove by filtration

Swift source code is provided below:


//
// ViewController.swift
// LocationManager
//
// Created by HEYANG on //.
// Copyright ©  years  HEYANG. All rights reserved.
//
import UIKit
//  You need to import CoreLocation The framework 
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
//  The statement 1 Global variables 
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
//  Set the positioning accuracy 
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//  Set the minimum distance of the location change   Range filter 
locationManager.distanceFilter = 
//  Sets the status of the request location 
if #available(iOS ., *) {
locationManager.requestWhenInUseAuthorization()
} else {
// Fallback on earlier versions
print("hello")
}// This is in ios Only after that 
//  Set the proxy to the current object 
locationManager.delegate = self;
if CLLocationManager.locationServicesEnabled(){
//  Enable location service 
locationManager.startUpdatingLocation()
}else{
print(" No location service ")
}
}
//  Locate the proxy method that failed to call 
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
//  Locate the proxy method invoked to update the geographic information 
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if locations.count > 
{
let locationInfo = locations.last!
let alert:UIAlertView = UIAlertView(title: " Get the geographic coordinates ",
message: " Longitude is: \(locationInfo.coordinate.longitude) , the dimensions are: \(locationInfo.coordinate.latitude)",
delegate: nil, cancelButtonTitle: " yes ")
alert.show()
}
}
}

The following is the source code of Objective-C:


//
// ViewController.m
// LocationManager
//
// Created by HEYANG on //.
// Copyright ©  years  HEYANG. All rights reserved.
//
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
/**  Globally positioned 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 the minimum distance of location change 
locationManager.distanceFilter = ;
//  Set the usage status of the location service 
[locationManager requestWhenInUseAuthorization]; 
locationManager.delegate = self;
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}else{
NSLog(@" Location-based services are not supported ");
}
self.locationManager = locationManager;
}
//  Locate the proxy method that failed to 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 , the dimensions are: %lf",coordinateD.longitude,coordinateD.latitude];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@" Displays the latitude and longitude of the current location "                 message:message delegate:nil cancelButtonTitle:@" cancel " otherButtonTitles:@" determine ", nil];
[alertView show];
}
}
@end 

The above is the site to share IOS introductory notes on the location system, I hope to help you.


Related articles: