Apple comes with map for positioning, Apple comes with map
Recently, the project encountered a need for map positioning. Considering that the project will become larger with a third-party library, the official map will be used.
This is the result diagram:
1. CoreLocation. frame is the framework used to detect user locations in the iPhone SDK.
1. To implement the positioning function, first introduce this framework. Then add two classes and one protocol (CLLocationManager, CLLocation, CllocationManagerDelegate ).
The precision level is set through the desiredAccuracy attribute of the Location Manager. The values can be found in the following table. The higher the accuracy level, the more electricity the phone consumes.
DesiredAccuracy Attribute Value |
Description |
KCLLocationAccuracyBest |
Best precision |
KCLLocationAccuracyNearestTenMeters |
Accurate to less than 10 meters |
KCLLocationAccuracyHundredMeters |
Accurate to less than 100 meters |
KCLLocationAccuracyKilometer |
Accurate to less than 1000 meters |
KCLLocationAccuracyThreeKilometers |
Accurate to less than 3000 meters |
2. Start the location manager for locating. [LocManager startUpdatingLocation] (you can also stop the detection location update [locManager stopUpdatingLocation])
3. Obtain Location Information
Coordinate is used to store the latitude and longpolling of a geographical location, representing the latitude and longitude of a geographical location, respectively.
Location is an instance object of the CLLocation class.
The altitude attribute indicates the altitude of a certain position. The returned value is a floating point type, which is extremely inaccurate in actual positioning.
The horizontalAccuracy attribute indicates horizontal accuracy, and the returned value is a floating point type. It is the radius of the circle centered on coordinate. The smaller the radius, the more accurate the positioning. If horizontalAccuracy is negative, it indicates that the Core Location fails to be located.
The verticalAccuracy attribute indicates the vertical horizontal accuracy, and the returned value is a floating point type. Its value is related to the value of altitude at the altitude, which is quite different from the actual situation.
4. CLLocationManagerDelegate Protocol
-(Void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray <CLLocation *> *) locations {}-(void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error {NSString * errorMessage; if ([error code] = kCLErrorDenied) {errorMessage = @ "Access Denied! ";} If ([error code] = kCLErrorLocationUnknown) {errorMessage = @" your location cannot be located! ";}}Protocol
2. Use MapKit to display a map
1. Using the MapKit. framework, you can easily embed a map in your application.
The region attribute is used to set which part of the map is displayed. It is a struct type.
Center is the value of coordinate, including the longitude and latitude information, which is used to represent the central position of the map.
Span indicates a span of a map. It includes the longitude and latitude variation information of the region, that is, the ratio of the scaled map.
The MapType attribute sets the map type. The values are listed in the following table.
MapType Attribute Value |
Description |
MKMapTypeStandard |
Standard street level Map |
MKMapTypeSatellite |
Satellite Map |
MKMapTypeHybrid |
Indicates the combination of the above two types |
2. Create a MKMapView object view and add it to the current controller view. Then, set map region and span in the two Location Manager proxies. In addition, you can set latitudeDelta and longitudeDelta to achieve scaling.
3. Add a map annotation and add a MapAnnotations class to the project. This class inherits from NSObject and follows the MKAnnotation protocol.
Code details:
1 # import <UIKit/UIKit. h> 2 3 @ interface ViewController: UIViewController4 5 6 @ endViewController. h 1 # import "ViewController. h "2 # import <CoreLocation/CoreLocation. h> 3 # import <MapKit/MapKit. h> 4 5 # import <MapKit/MKAnnotation. h> 6 # import "MapAnnotations. h "7 8 @ interface ViewController () <CLLocationManagerDelegate, MKMapViewDelegate> 9 {10 CLLocationManager * locManager; 11 CLLocationCoordinate2D loc; 12 UITextView * textView; 13 14 MKMapView * map; 15 16 MapAnnotations * mapAnn Otations; 17} 18 @ end19 20 @ implementation ViewController21 22-(void) viewDidLoad {23 [super viewDidLoad]; 24 // Do any additional setup after loading the view, typically from a nib.25 // create a location manager 26 locManager = [[CLLocationManager alloc] init]; 27 locManager. delegate = self; 28 if ([CLLocationManager locationServicesEnabled]) {29 locManager. desiredAccuracy = kCLLocationAccuracyBest; // The best precision is 30 locManag. Er. distanceFilter = 300; // The distance from the filter is 300 meters 31 [locManager startUpdatingLocation]; // start the location manager to locate 32 [locManager requestAlwaysAuthorization]; 33} 34 35 map = [[MKMapView alloc] initWithFrame: CGRectMake (0, 0, [UIScreen mainScreen]. bounds. size. width, [UIScreen mainScreen]. bounds. size. height)]; 36 map. showsUserLocation = YES; 37 [self. view addSubview: map]; 38 39 UIButton * addPanBtn = [UIButton buttonWithType: UIB UttonTypeContactAdd]; 40 addPanBtn. center = CGPointMake (40, 40); 41 [addPanBtn addTarget: self action: @ selector (addPin :) forControlEvents: UIControlEventTouchUpInside]; 42 [self. view addSubview: addPanBtn]; 43} 44 45 # pragma mark-proxy method 46 // This is the location update method. When our range of movement is greater than the value of the distance filter, location manager will call this method to relocate 47-(void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray <CLLocation *> *) locations {48 CLLocati On * location = [locations firstObject]; 49 50 NSLog (@ "latitude: % f, longpolling: % f", location. coordinate. latitude, location. coordinate. longpolling); 51 52 MKCoordinateRegion region; 53 MKCoordinateSpan span; 54 span. latitudeDelta = 1; 55 span. longitudeDelta = 1; 56 region. span = span; 57 region. center = location. coordinate; 58 [map setRegion: region animated: YES]; 59 [map regionThatFits: region]; 60 61 mapAnnotati Ons = [[MapAnnotations alloc] initWithCoordinate: location. coordinate]; 62 mapAnnotations. title = @ "TEST"; 63 mapAnnotations. subtitle = @ "Just For Test"; 64 [map addAnnotation: mapAnnotations]; 65} 66 67-(void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error {68 NSString * errorMessage; 69 if ([error code] = kCLErrorDenied) {70 errorMessage = @ "Access Denied! "; 71} 72 if ([error code] = kCLErrorLocationUnknown) {73 errorMessage = @" your location cannot be located! "; 74} 75} 76 77-(void) dealloc {78 // stop detection location update 79 [locManager stopUpdatingLocation]; 80} 81 82-(void) addPin :( id) sender {83 mapAnnotations = [[MapAnnotations alloc] initWithCoordinate: map. region. center]; 84 mapAnnotations. title = [NSString stringWithFormat: @ "% f", map. region. center. latitude]; 85 mapAnnotations. subtitle = [NSString stringWithFormat: @ "% f", map. region. center. longpolling]; 86 [map addAnnotation: mapAnnotations]; 87} 88 89-(void) didReceiveMemoryWarning {90 [super didreceivemorywarning]; 91 // Dispose of any resources that can be recreated.92} 93 94 @ endViewController. m 1 # import <Foundation/Foundation. h> 2 # import <CoreLocation/CoreLocation. h> 3 # import <MapKit/MapKit. h> 4 5 @ interface MapAnnotations: NSObject <MKAnnotation> 6 7 @ property (nonatomic, copy) NSString * title; 8 @ property (nonatomic, copy) NSString * subtitle; 9 @ property (nonatomic, assign) CLLocationCoordinate2D coordinate; 10 11-(instancetype) initWithCoordinate :( CLLocationCoordinate2D) c; 12 13 @ endMapAnnotations. h 1 # import "MapAnnotations. h "2 3 @ implementation MapAnnotations 4 5-(instancetype) initWithCoordinate :( CLLocationCoordinate2D) c {6 _ coordinate = c; 7 return self; 8} 9 10 @ endMapAnnotation. m