The main class of the "iOS Dev-110" Mapkit framework Mkmapview and Proxy methods, use of pins addannotation

Source: Internet
Author: User

#import "ViewController.h" #import <MapKit/MapKit.h> #import <CoreLocation/CoreLocation.h> @interface Viewcontroller () <MKMapViewDelegate> @property (nonatomic,strong) Cllocationmanager *locmgr; @property (Weak, nonatomic) Iboutlet Mkmapview *mapview; @end @implementation viewcontroller-(Cllocationmanager *) locMgr{if (_locMgr==    Nil) {_locmgr=[[cllocationmanager alloc]init]; } return _locmgr;}    -(void) viewdidload {[Super viewdidload];    if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) {[Self.locmgr requestwheninuseauthorization]; }//Display user location (blue glowing circle), and none and followwithheading two, when this attribute is iOS8, the first time the map is opened, it will automatically locate and display this position.    Not on the IOS7 simulator.        Self.mapview.usertrackingmode=mkusertrackingmodefollow;        Map mode, default is Standard mode, there are satellite mode satellite and hybrid mode hybrid Self.mapview.maptype=mkmaptypestandard; Set proxy self.mapview.delegate=self;} Mkuserlocation is a pin model on the map with title and subtitle and location information. The method call is frequent-(void) Mapview: (Mkmapview *) Mapview DIDUPDATeuserlocation: (mkuserlocation *) userlocation{NSLog (@ "%f,%f", UserLocation.location.coordinate.longitude,    UserLocation.location.coordinate.latitude);    Click on the PIN, the following information will appear [email protected] "China";        [email protected] "One of the four ancient civilizations"; Let the map show the user's location (IOS8 opens the map by default to the map where the user is located), the method cannot set the map accuracy//[Mapview setcentercoordinate:        UserLocation.location.coordinate Animated:yes];    This method sets the map accuracy and shows the map where the user is located Mkcoordinatespan span=mkcoordinatespanmake (0.1, 0.1);    Mkcoordinateregion Region=mkcoordinateregionmake (userLocation.location.coordinate, span); [Mapview setregion:region animated:yes];} You can print the span of latitude and longitude to test how much the current view has a latitude and longitude span, and then use it in the Mkcoordinatespanmake method above-(void) Mapview: (Mkmapview *) Mapview Regiondidchangeanimated: (BOOL) animated{NSLog (@ "%f,%f", MapView.region.span.latitudeDelta, MapView.region.span.longitudeDelta);} @end

(1) If it is a iOS8 simulator, the following error will appear:

Trying to start Mapkit location updates without prompting for location authorization. Must call-[cllocationmanager requestwheninuseauthorization] or-[cllocationmanager requestalwaysauthorization] First .


You need to add the following, and you need to add nslocationwheninuseusagedescription in plist to Yes:

if ([[Uidevice currentdevice].systemversion doublevalue]>=8.0) {        [self.locmgr requestwheninuseauthorization] ;    }

(2) Agent method

Commonly used is the user location update, the proxy method called when the zone changes.


(3) Questions

--When displaying the map where the user is located, the user's location is sometimes not in the middle, but on the right. Is it a bug in the simulator?


(4) Add a button that returns to the current location, adding a method:

-(Ibaction) Backtouserlocation: (ID) Sender {    [Self.mapview setcentercoordinate: Self.mapView.userLocation.location.coordinate Animated:yes];}

(5) Add a PIN

The General order is: Create a Pin object >>> set the coordinates of the PIN, title and sub-title >>> finally add the pin to the map

The core is: The system comes with a pin can not set the coordinates (read-only), so we generally write a model of inheritance NSObject, the Coordinate,title,subtile attribute is written in, the key is that this model needs to follow <mkannotation >. Then use this pin class to create a Pin object. The following is a custom pin class.

#import <Foundation/Foundation.h> #import <MapKit/MapKit.h> @interface wpannotation:nsobject< Mkannotation> @property (nonatomic, assign) cllocationcoordinate2d coordinate; @property (nonatomic, copy) NSString * Title: @property (nonatomic, copy) NSString *subtitle; @end

Then use:

-(void) viewdidload {    [super viewdidload];    ......    Monitor click, Pin    [Self.mapview addgesturerecognizer:[[uitapgesturerecognizer alloc] initwithtarget:self action:@ Selector (Tapmapview:)];} -(void) Tapmapview: (UITapGestureRecognizer *) tap{    //Get click Point    cgpoint point=[tap LocationInView:tap.view];    Point converted to coordinates    cllocationcoordinate2d Coordi=[self.mapview convertpoint:point ToCoordinateFromView:self.mapView];    Create a PIN model, set the coordinates of the pin, and add the pin to    wpannotation *anno=[[wpannotation Alloc]init];    Anno.coordinate=coordi;    [Email protected] "Humble administration garden";    [email protected] "near Suzhou Museum";    [Self.mapview Addannotation:anno];}

The main class of the "iOS Dev-110" Mapkit framework Mkmapview and Proxy methods, use of pins addannotation

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.