In an iOS application, we can develop a map application using the Map Kit API, the core of which is the Mkmapview class. The main implementation of this function is to display the map, add labels, tracking user location changes.
(All contents of this log are based on the successful acquisition of the coordinates location, get the method see the previous log)
1. Show Map
First you need to refer to <mapkit/mapkit.h>, and its principal agreement is mkmapviewdelegate.
Initialize Mapview
Mapview = [[Mkmapview alloc] Initwithframe:cgrectmake (5, +, SELF.VIEW.FRAME.SIZE.WIDTH-10, 430)]; Mapview.maptype = Mkmaptypestandard; Mapview.delegate = self;
The map type (MAPTYPE) has the following 3 kinds:
Mkmaptypestandard Labeling Map Types
Mkmapsatellite Satellite map types
Mkmaptypehybrid Mixed Map Types
After getting the placemark of the location, I customized a function to implement the map display
-(void) Showinmapview: (Clplacemark *) placemark{ cllocationcoordinate2d coordinate = Placemark.location.coordinate; Add Mapview Mkcoordinatespan span = Mkcoordinatespanmake (0.01, 0.01); <span style= "White-space:pre" ></ span>//span (scale) mkcoordinateregion region = mkcoordinateregionmake (coordinate, span); Scope, Area [Mapview setregion:region]; [Self.view Addsubview:mapview];}
Here you simply set the display area of the Mapview, using the following structure:
typedef struct{
Cllocationcoordinate2d Center; //Center point
Mkcoordinatespan Span; //span
}mkcoodinateregion;
struct initialization uses the Mkcoodinateregionmake function, whose first parameter specifies the center point of the target region, and the second sets the span of the target area
The structure of the display span, Mkcoordinatespan, is defined as:
typedef struct{
Cllocationdegress Latitudedelta; //area north-south span
Cllocationdegress Longtitudedelta; //area of the thing span
}mkcoordinatespan;
The north-south span of 1 degrees is about 111 km, the east-west span at the equator 1 degrees is about 111 km, the closer to the poles, this distance is gradually reduced, at the pole is changed to 0 km.
Finally, use the Setregion function to set the display area for the Mapview.
2. Adding annotations
First of all to refer to its header file <mapkit/mkannotation.h>
First set the callout point, you need to use the Mkpointannotation class, it mainly has the following three properties
-(NSString *) Titile; The main title of the callout point
-(NSString *) subtitle; subtitle of Callout Point
-(cllocationcoordinate2d) coordinate;//location information of callout points
After setting the above three properties, add the callout point to the Mapview directly using the Addannotation function.
This part of the code is also in the above showinmapview: function, the code is as follows
-(void) Showinmapview: (Clplacemark *) placemark{ cllocationcoordinate2d coordinate = Placemark.location.coordinate; Add Mapview Mkcoordinatespan span = Mkcoordinatespanmake (0.01, 0.01); Mkcoordinateregion region = mkcoordinateregionmake (coordinate, span); [Mapview setregion:region Animated:yes]; [Self.view Addsubview:mapview]; Add annotation mkpointannotation *annotaion = [[Mkpointannotation alloc] init]; annotaion.coordinate = coordinate; Annotaion.title = placemark.locality; Annotaion.subtitle = Placemark.name; [Mapview addannotation:annotaion];}
To implement the display of annotations, we should also implement the Mapview:viewforannotation: method in the Map view delegation protocol, in which we instantiate a callout point view (Mkpointannotationview) and complete its initialization work, The implementation code is as follows:
-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id <MKAnnotation>) annotation{ Mkpinannotationview *annotaionview = (Mkpinannotationview *) [Mapview dequeuereusableannotationviewwithidentifier:@] Pin_annotation "]; if (Annotaionview = = nil) { Annotaionview = [[Mkpinannotationview alloc] Initwithannotation:annotation reuseidentifier:@ "Pin_annotation"]; } Annotaionview.pincolor = Mkpinannotationcolorred;<span style= "White-space:pre" ></span>//callout point color Annotaionview.animatesdrop = Yes;<span style= "white-space:pre" ></span>//animation Annotaionview.canshowcallout = Yes;<span style= "White-space:pre" ></span>//illustration number return Annotaionview;}
WHERE (Mbannotationview *)-Dequeuereusableannotationviewwithidentifier: Returns a reusable callout view with an identifier
3, tracking User location changes
Open the Showsuserlocation property of the map and set the method Setusertrackmode: You can track the user's position and direction changes.
Mapview.showsuserlocation = YES; [Mapview setusertrackingmode:mkusertrackingmodefollowwithheading Animated:yes];
The following three types of user tracking modes are available:
Mkusertrackingmodenone no user tracking mode
Mkusettrackingmodefollow tracking users ' location changes
Mkusertrackingmodefollowwithheading Tracking User location and direction changes
You also need to implement the delegate method for the map view mapview:didupdatauserlocation:
-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{ Mapview.centercoordinate = userLocation.location.coordinate;}
iOS Learning Note--mapkit