To use map nails, we first need to create an annotation class for map nails. The Code is as follows:
@ Interface myannotation: nsobject <mkannotation> // implements the mkannotation method. This method is called when the map nail is dragged or actively called to change the location of the map nail. -(Void) setcoordinate :( cllocationcoordinate2d) newcoordinate;
Annotation not only records the location of the map nail, but also includes the property title and the property subtitle ).
@ Implementation myannotation // map position @ synthesize coordinate; // function for obtaining the title value-(nsstring *) Title {return "";} // implement the function for obtaining the subtitle value-(nsstring *) Subtitle {return "I Am a sub-name ";}
In this way, when we click the map to select a map
Next, we will add a map nail. In the viewcontroller's viewdidload function on the page, we will add the following code
-(Void) viewdidload {//... Myannotation * annotation = [[myannotation alloc] init]; // design location (path and latitude) [annotation setcoordinate: cllocationcoordinate2dmake (32.632, 120.902)]; // Add a dingtalk to the map. m_mapview is the control variable of mkmapview [m_mapview addannotation: m_annotation];}
So far, we have been dealing with the map's note object myannotation. This object only records the location and name, not what we want. Next, the dingtalk is coming soon.
First, we need viewcontroller to implement the mkmapviewdelegate protocol (Protocol), and then implement the following function, which will be called after adding a map nail to the code above.
-(Mkannotationview *) mapview :( mkmapview *) mapview viewforannotation :( id <mkannotation>) annotation {static nsstring * annotationid = @ "mylocation"; if (! [Annotation iskindofclass: [myanotation class]) {return nil;} mkpinannotationview * pinview = (mkpinannotationview *) [mapview comment: annotationid]; If (pinview = nil) {// create a map nail. The mkpinannotationview object pinview = [[[mkpinannotationview alloc] initwithannotation: annotation reuseidentifier: annotationid] autorelease]; pinview. animatesdrop = yes; // use the animation effect pinview when the map is hit. canshowcallout = yes; // The pinview name is displayed after the map is selected. draggable = yes; // map nails can be dragged} else {// map nails have already created pinview. annotation = annotation;} return pinview ;}
Now, we have successfully added a map to the map. We can use m_annotation.coordinate to obtain the path and latitude of the current map.
Then, we can get the Geographic Name (street, shop), the route between the current location, and so on. We will introduce the content later.