SoFunction
Updated on 2025-04-12

IOS Introduction Notes Geographical Location Positioning System

Preface: Regarding geographical location and positioning system, it is also common in iOS development. For example, when searching for restaurant stores outside Meituan, it first requires the user's current mobile phone location, and then search for the location of the relevant restaurant stores near this location, and provide relevant catering information. For example, the most common one is map navigation, which requires location services, and then select a route based on the user's destination. In fact, as a mobile phone user for so long, you will more or less find that after some app applications are successfully installed on your phone for the first time, they may prompt information such as "whether to agree to XXx (such as Baidu browser) to obtain the current location" for the first time. It can be seen that geographical location and positioning systems are essential skills for enterprise app development.

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

Tips for writing before formal study:

This is due to the problem of setting positioning permissions caused by the xcode upgrade.
After upgrading xcode6 and xcode7, open the previous xcode5 project, but the program cannot be located. When upgrading the project to xcode6 or xcode7, you need to write authorization by yourself, otherwise you will not have permission to locate it.

Solution:

First add the corresponding default field to , and set the value to YES (the front-end positioning writes the upper field, and the front-end positioning writes the lower field)
NSLocationWhenInUseUsageDescription //Allows to get the description of GPS in the foreground
NSLocationAlwaysUsageDescription //Allows to get GPS description in front and backend

Settings illustration:


OK, if it is set up, then officially enter coding learning. First, be familiar with the categories, methods and attributes related to positioning services provided by Apple:

1. Introduction to positioning services and map applications

Positioning service: Obtain the user's current location information and perform relevant data processing based on the user's location information.

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

•What to master in positioning services:

•The main operation class: CLLocationManager

•Affiliated library: CoreLocation

•Structure: CLLocationCoordinate2D (latitude and longitude), CLCLocationCoorRegion (region)

•What you need to master in map applications:

•Framework: MapKit

•Operation class: MKMapView

2. Positioning Service

•property:

• desiredAccuracy sets positioning accuracy, which is a constant property, generally best
•distanceFilter Minimum change distance for repositioning

method:

•Set the status of when to enable positioning •requestAlwaysAuthorization() Always to enable positioning
• requestWhenInUseAuthorization() Turn on positioning when the app enters the foreground (new method for iOS8)
•Class method locationServicesEnabled() Does it have location service function (CLLocationManager)
•startUpdatingLocation() Turn on positioning

acting:

•Agent protocol:
•Proxy method: You can directly enter the API of this library to view it, as long as it is to locate the proxy method called incorrectly, locate the proxy method called successfully, etc.;

Objects involved

•locations: CLLocation Properties of the CLLocation object: •coordinate •longitude/latitude

English vocabulary accumulation:

•accuracy English 'ækjʊrəsɪ n. [number] Accuracy, accuracy
•filter 'fɪltə Filter Filter; filter; filter Filter; permeation; remove by filtration

The following is the Swift source code:

//
// 
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 2019 HEYANG. All rights reserved.//
import UIKit
// CoreLocation framework needs to be importedimport CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
// Declare a global variablevar locationManager:CLLocationManager!
override func viewDidLoad() {
()
locationManager = CLLocationManager()
// Set the accuracy of positioning = kCLLocationAccuracyBest
// Set the minimum distance for positioning changes Distance filter = 
// Set the status of requested positioningif #available(iOS ., *) {
()
} else {
// Fallback on earlier versions
print("hello")
}//This is only available after ios// Set the proxy as the current object = self;
if (){
// Turn on positioning service()
}else{
print("No location service")
}
}
// Proxy method for failed callfunc locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
// Proxy method for positioning and updating geographic information callsfunc locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if  > 
{
let locationInfo = !
let alert:UIAlertView = UIAlertView(title: "Geographical coordinates obtained",
message: "Longitude is:\(),The dimension is:\()",
delegate: nil, cancelButtonTitle: "Yes")
()
}
}
}

Here is the source code of Objective-C:

//
// 
// LocationManager
//
// Created by HEYANG on //.
// Copyright © 2019 HEYANG. All rights reserved.//
#import ""
#import <CoreLocation/>
@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 = kCLLocationAccuracyBest;
// Set the minimum distance to change positioning = ;
// Set the usage status of the location service[locationManager requestWhenInUseAuthorization]; 
 = self;
if ([CLLocationManager locationServicesEnabled]) {
[locationManager startUpdatingLocation];
}else{
NSLog(@"This machine does not support the location service function");
}
 = locationManager;
}
// Proxy method for failed call-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"error message:%@",error);
}
// Proxy method for positioning data update calls-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
if ( > ) {
CLLocation* location = ;
CLLocationCoordinateD coordinateD = ;
NSString* message = [NSString stringWithFormat:@"Longitude: %lf, dimension is: %lf",,];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Show the latitude and longitude of the current location" message:message delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Sure", nil];
[alertView show];
}
}
@end 

The above is the geographic location positioning system of the IOS introductory notes shared by the editor. I hope it will be helpful to everyone.