Simple operation for native map positioning Mapkit use

Source: Internet
Author: User

//

Viewcontroller.m

Demo_1 Mkmapview

//

Created by Tareba on 15/12/16.

copyright©2015 year Tanada. All rights reserved.

//

#import "ViewController.h"

#import <MapKit/MapKit.h>

#import "TRAnnotation.h"

@interface Viewcontroller () <mkmapviewdelegate>

@property (weak, nonatomic) iboutlet mkmapview *mapview;

/**

* same point : ask the user for permission

*/

@property (nonatomic,strong)Cllocationmanager *manager;

-(ibaction) addannotation: (UIButton *) sender;

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

// Initialize manger

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

// ask for permission from the user

[self. Manager requestwheninuseauthorization];

//delegate

self. Mapview. Delegate=self;

// set map not allowed to rotate

self. Mapview. rotateenabled=NO;

// Set the display type of the map ( satellite / Shore quasi map / hybrid )

//self.mapview.maptype=mkmaptypehybrid;

// start positioning

self. Mapview. Usertrackingmode=mkusertrackingmodefollow;

}

#pragma mark-mapviewdelegate

has been positioned to the user's location and finished displaying

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

NSLog(@ " dimension %f, precision %f", userlocation. Location. Coordinate. Latitude, userlocation. Location. Coordinate. Longitude);

Userlocation. title=@ " User location ";

Userlocation. subtitle=@ " description information ";

}

-(void) Mapview: (mkmapview *) Mapview regionwillchangeanimated: (BOOL) animated{

NSLog(@ "The map is going to move ");

}

Make sure the map has moved

-(void) Mapview: (mkmapview *) Mapview regiondidchangeanimated: (BOOL) animated{

NSLog(@ "The map has moved ");

}

-(ibaction) addannotation: (UIButton *) Sender {

// Create model class

// Compliance Agreement

//.h Declaration of three attributes 1 must , two Optional

// Add to map view ( mkannotation protocol must be followed )

cllocationdegrees latitude = 35.123+arc4random_uniform(ten); 0~10

cllocationdegrees longitude = 116.125+arc4random_uniform();

trannotation *annotation=[trannotation new];

Annotation. coordinate=cllocationcoordinate2dmake(latitude, longitude);

// Set the display area of the map view ( pins are displayed in the center of the View )

// span

mkcoordinatespan span=mkcoordinatespanmake(0.5, 0.52);

// Center position and span

mkcoordinateregion region=mkcoordinateregionmake(annotation. Coordinate, span);

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

[self. Mapview addannotation: annotation];

}

@end

#import <Foundation/Foundation.h>

#import <MapKit/MapKit.h>

@interface trannotation: nsobject<mkannotation>

@property (nonatomic, assign) cllocationcoordinate2d coordinate;

@property (nonatomic, copy) nsstring *title;

@property (nonatomic, copy) nsstring *subtitle;

@end

Simple operation for native map positioning Mapkit use

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.