#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