iOS Learning _ map _ Add a Pin

Source: Internet
Author: User

The first step is to introduce #import <MapKit/MapKit.h> Framework

Creates a class that inherits and NSObject; declares a property

Next, implement the related operations in Viewcontroller

Introducing Header Files

#import <MapKit/MapKit.h><CoreLocation/CoreLocation.h>"  MyAnnotation.h"

Define the appropriate properties

// Location Manager @property (nonatomic,strong) Cllocationmanager *Locationmanager; // Displays the view of the map @property (nonatomic,strong) Mkmapview *mapview;

Code manipulation

#pragmaMark-Create View-(void) createmapview{//Create a map and add it to the current viewSelf.mapview =[[Mkmapview alloc] Initwithframe:[uiscreen mainscreen].bounds];    [Self.view AddSubview:self.mapView]; //Set up proxy_mapview.Delegate=Self ; //positioningSelf.locationmanager =[[Cllocationmanager alloc] init]; //Make privacy Judgments    if(![Cllocationmanager locationservicesenabled]) {NSLog (@"Current device location is not available"); }    //Determine authorization status    if([Cllocationmanager authorizationstatus]! =kclauthorizationstatusauthorizedwheninuse) {        //you need to set what is allowed to be located in Inforplist before authorizing the request: Nslocationwheninuseusagedescription (Nslocationalwaysusagedescription this is another way )[Self.locationmanager requestwheninuseauthorization]; }    //set location tracking for a map_mapview.usertrackingmode =Mkusertrackingmodefollow; //set the display type of the map_mapview.maptype =Mkmaptypestandard; //Add a PIN[self addannotation];}

Add a PIN

#pragmaMark-Add a pin-(void) addannotation{//Set LocationCllocationcoordinate2d location1 = Cllocationcoordinate2dmake ( +, the); Myannotation*annotation =[[Myannotation alloc] init]; Annotation.coordinate=Location1; Annotation.title=@"Beijing"; Annotation.subtitle=@"ANN";#warningAdd a pictureAnnotation.image= [UIImage imagenamed:@"1"];        [_mapview addannotation:annotation]; Cllocationcoordinate2d Location2= Cllocationcoordinate2dmake ( the,121); Myannotation*annotation1 =[[Myannotation alloc] init]; Annotation1.coordinate=Location2; Annotation1.title=@"Dalian"; Annotation1.subtitle=@"Abxa"; Annotation1.image= [UIImage imagenamed:@"1"];            [_mapview Addannotation:annotation1]; Cllocationcoordinate2d Location3= Cllocationcoordinate2dmake ( to,121); Myannotation*annotation2 =[[Myannotation alloc] init]; Annotation2.coordinate=Location3; Annotation2.title=@"Shanghai"; Annotation2.subtitle=@"Gda"; Annotation2.image= [UIImage imagenamed:@"1"]; [_mapview Addannotation:annotation2];}

Implementing Proxy methods

#pragmaMark-Proxy method for implementing a custom pin view//It is called when a pin is displayed .-(Mkannotationview *) Mapview: (Mkmapview *) Mapview viewforannotation: (id<mkannotation>) annotation{//determine if the Pin class is currently customized    if([Annotation iskindofclass:[myannotationclass]]) {        //define a reuse identity        StaticNSString *identifier =@"Annotationone"; Mkannotationview*annotationview =[_mapview Dequeuereusableannotationviewwithidentifier:identifier]; if(!Annotationview) {Annotationview=[[Mkannotationview alloc] initwithannotation:annotation reuseidentifier:identifier]; //Allow user interactionAnnotationview.canshowcallout =YES; //set the header offset for details and pinsAnnotationview.calloutoffset = Cgpointmake (0,2); //set the left view of the detailsAnnotationview.leftcalloutaccessoryview = [[Uiimageview alloc] initwithimage:[uiimage imagenamed:@"1"]]; }        //Modify the Pin viewAnnotationview.annotation =annotation; Annotationview.image= ((Myannotation *). Annotation). Image; returnAnnotationview; } Else {        returnNil; }}

iOS Learning _ map _ Add a Pin

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.