Positioning requires adding nslocationwheninuseusagedescription in info
if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) { //Get Permissions [Self.locationmanager Requestwheninuseauthorization]; } Start positioning [Self.locationmanager startupdatinglocation];//Lazy Load-(Cllocationmanager *) locationmanager{ if (!_ Locationmanager) { _locationmanager=[[cllocationmanager alloc]init]; _locationmanager.delegate=self; } return _locationmanager; }
Create a map
Self.mapview =[[mkmapview Alloc]initwithframe:[[uiscreen Mainscreen]bounds]; 1. Track user location (show user's specific location) Self.mapView.userTrackingMode =mkusertrackingmodefollow; 2. Set the map type//Self.mapview.maptype=mkmaptypestandard; Set proxy self.mapview.delegate=self; Self.mapview.showsuserlocation=yes; [Self.view AddSubview:self.mapView]; #pragma mark-nkmapviewdelegate//when the user's location is updated, it is called (constantly monitoring the user's location, the call frequency is particularly high)-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{//Mkuserlocation Pin Model object NSLog (@ "%f%f", UserLocation.location.coordinate.longitude, UserLocation.location.coordinate.latitude); [email protected] "Days gray wild wind blowing grassland see cattle and sheep"; [email protected] "bed front light, ground frost"; Set the center point of the map (the area where the user is located)//[Mapview setCenterCoordinate:userLocation.location.coordinate Animated:yes];] Set the display range of the map//Set the span Mkcoordinatespan span =mkcoordinatespanmake (0.5,0.5); Cllocationcoordinate2d Center = UserLocation.location.coordinatE Mapview.region=mkcoordinateregionmake (center, span);} Get current span-(void) Mapview: (Mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated{NSLog (@ "%f%f", Mapview.regi On.span.latitudedelta,mapview.region.span.longitudedelta);}
Add a PIN (define your own to create a class that adheres to the Mkannotation protocol)
#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface wbannotation:nsobject< Mkannotation> @property (nonatomic,assign) cllocationcoordinate2d coordinate; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *subtitle;
Create a PIN
Create gesture UITapGestureRecognizer * longpress =[[uitapgesturerecognizer alloc]initwithtarget:self action: @selector ( Tapmapview:)]; Add gesture [Self.mapview addgesturerecognizer:longpress];//response gesture Event-(void) Tapmapview: (UITapGestureRecognizer *) tap { //1. Get the location where the user clicked on the map (x, y) cgpoint point =[tap LocationInView:tap.view]; 2. Convert mathematical coordinates to geographic latitude and longitude coordinates cllocationcoordinate2d coordinate= [Self.mapview convertpoint:point Tocoordinatefromview: Self.mapview]; 3. Create a PIN model, add a PIN to the map (custom pin Class) wbannotation *anno =[[wbannotation alloc]init]; Anno.coordinate =coordinate; [Email protected] "Big Brother"; [Email protected] "Hello great"; [Self.mapview Addannotation:anno]; }
IOS Maps (Mkmapview)