First create a Viewcontroller:
Add Mapkit,corelocation Frame
First, add a map
1. Initialize the map view:
@property (nonatomic, Strong) Mkmapview *=[[mkmapview alloc]initwithframe:self.view.bounds];
2, set the display type of the map, three kinds: standard, satellite map, mixed
Self.mapView.mapType { 0, mkmaptypesatellite, Mkmaptypehybrid};
3, define whether the map can zoom in and out slide, add agent
self.mapView.zoomEnabled == YES;
Self.mapView.delegate = self;
4. Add Corelocation Frame
Add geographic coordinates for a region (latitude and longitude), north latitude is positive, longitude is positive; current coordinates represent Zhengzhou position
34.7598711113.663221;
5, indicating the accuracy of the display area, the smaller the value represents the more accurate the range, the larger the value represents the greater the range, but not accurate
Mkcoordinatespan span = {$,+}; 100 on the map representsthe range of 111 km
6 . Create a value representing the range based on geographic coordinates and span . (usually a circle, and a radius)
Initialization method (1): mkcoordinateregion region = {Coordinate,span}; (2): mkcoordinateregion region; =mkcoordinateregionmake (Coordinate,span);
7, tell the map to show the area, and speak the map added to the current view
[Self.mapview Setregion:region];[self.view AddSubview:self.mapView];
8. Add annotations for coordinate locations
Mkpointannotation *pinannotation ==@ " Zhengzhou "@ " Henan Qingyun Information Technology "; [Self.mapview addannotation:pinannotation];
9, when you want to display when the annotation customization, call the following method, used to set the display interface
-(mkannotationview*) Mapview: (Mkmapview *) Mapview viewforannotation: (ID<MKAnnotation>) annotation{if([Annotation iskindofclass:[mkuserlocationclass]]) { returnNil; } StaticNSString *indentifier =@"Pinview"; Mkpinannotationview*pinview = (mkpinannotationview*) [Mapview Dequeuereusableannotationviewwithidentifier:indentifier]; if(Nil = =Pinview) {Pinview=[[Mkpinannotationview alloc] initwithannotation:annotation reuseidentifier:indentifier]; } Pinview.pincolor=mkpinannotationcolorpurple; Pinview.animatesdrop=YES; Pinview.canshowcallout=YES; returnPinview;}
10, the difference between mkannotation and Mkannotationview
Mkannotation:
It is actually a model in the annotations, which is mainly used to provide the corresponding data to the view. By default, there are three properties: 1?title 2.subtitle 3, coordinate
Mkannotationview:
In the annotations, it is used to describe how the annotations are displayed, in other words whether they are displayed as blue dots or as pins. In the delegate method to determine, if the delegate method, returns nil, this time the blue dot. If you want to display other views, you must implement them in your code.
Second, add location management
1, initialize, and add the agent, set the precision
@property (nonatomic, Strong) Cllocationmanager *Locationmanager; = [[Cllocationmanager alloc] init]; Self.locationmanager. delegate = self ; - ;// for location management, it is not the higher the accuracy the better. Because the GPS module is the most energy-consuming module of the mobile phone, it is necessary to set specific precision according to the specific application to maximize the power consumption of our application.
= kcllocationaccuracybest; [Self.locationmanager startupdatinglocation];
2, when the location update call the following method, you can get the new and old two positions between the distance, the new position of the coordinates; and the map is displayed in the new coordinate location
- (void) Locationmanager: (Cllocationmanager *) Manager didupdatetolocation: (cllocation*) newlocation fromlocation: (cllocation*) oldlocation{NSLog (@"%@", Nsstringfromselector (_cmd));cllocationdistance Distance=[NewLocation distancefromlocation:oldlocation]; NSLog (@"Distance is%f", Distance/1000.0); Mkcoordinateregion Region= Mkcoordinateregionmake (Newlocation.coordinate, Mkcoordinatespanmake ( -, -)); [Self.mapview setregion:region Animated:yes]; }
3. If the location manager fails, the following method is called and an error message is returned
-(void) Locationmanager: (Cllocationmanager *) Manager *) error{ NSLog (@ " %@", error);}
Summary: About the basic implementation of maps and positioning in iOS first described here, follow-up updates, welcome to explore the query!