[IOS] using map nails in mapview

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.