IOS Maps (Mkmapview)

Source: Internet
Author: User

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)

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.