Mapkit Add a Pin

Source: Internet
Author: User

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "MYAnnotation.h"

@interface Viewcontroller () <mkmapviewdelegate>

@property(nonatomic,strong)Cllocationmanager *mag;

@property(nonatomic,strong)clgeocoder *ceocoder;

@property(nonatomic,strong)Mkmapview*mapview;

@end

@implementation Viewcontroller

-(mkmapview*) Mapview

{

if (! _mapview) {

_mapview=[[mkmapview alloc]init];

}

return _mapview;

}

-(void) viewdidload {

[Super viewdidload];

self. Mag=[[cllocationmanager alloc]init];

[self. Mag requestalwaysauthorization];

[self. Mag requestwheninuseauthorization];

// must be logged or user rights verification will not be performed

Cllocationmanager *mags=[[cllocationmanager Alloc]init];

[Mags requestalwaysauthorization];

[Mags requestwheninuseauthorization];

self. Mapview. Frame=self. View. Bounds;

//self.mapview.visiblemaprect=mkmaprectmake (a);

[self. View addsubview:self. Mapview];

// set up a proxy for the map

self. Mapview. Delegate=self;

self. Mapview. Showsuserlocation=YES;

self. Mapview. Usertrackingmode=mkusertrackingmodefollow;

}

Update user Information

-(void) Mapview: (mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation

{

[self. Ceocoder reversegeocodelocation: userlocation. Location completionhandler: ^ (nsarray *placemarks, nserror *error) {

clplacemark *mark=[placemarks firstobject];

Userlocation. title=mark. Name;

Userlocation. subtitle=mark. Locality;

}];

// Set the area of the map display

cllocationcoordinate2d center=userlocation. Location. coordinate;

// specify latitude and longitude span

mkcoordinatespan span=mkcoordinatespanmake(a );

// Use the user's current location as the center

mkcoordinateregion region=mkcoordinateregionmake(center, Span);

// Set display area

[self. Mapview setregion: Region animated:YES];

}

Load a pin

-(mkannotationview *) Mapview: (mkmapview *) Mapview viewforannotation: (ID< Mkannotation>) annotation

{

if ([annotation iskindofclass: [mkuserlocation class]]) {

return nil;

}

static nsstring*id=@ "anno";

mkpinannotationview *annotationview= (mkpinannotationview*) [Mapview Dequeuereusableannotationviewwithidentifier: ID];

if (annotationview==nil) {

annotationview=[[mkpinannotationview alloc]initwithannotation: annotation Reuseidentifier: ID];

}

// Set the color of the pin

Annotationview. Pincolor=mkpinannotationcolorpurple;

// Set pin animation

Annotationview. Animatesdrop=YES;

return Annotationview;

}

-(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event

{

cgpoint point=[[touches anyobject] locationinview:self. Mapview];

cllocationcoordinate2d coordinate=[self. Mapview convertpoint:p oint tocoordinatefromview:self. Mapview];

// Add a pin

myannotation *anno=[[myannotation alloc]init];

Anno. title=@ " what you want ";

Anno. subtitle=@ " can parse out position with Clgeocoder ";

Anno. coordinate=coordinate;

[self. Mapview addannotation: anno];

@end

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface myannotation: nsobject<mkannotation>

@property(nonatomic,assign)cllocationcoordinate2d coordinate;

@property(nonatomic,copy)nsstring*title;

@property(nonatomic,copy)nsstring*subtitle;

@end

@implementation myannotation

@end

Mapkit 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.