The use of Mapkit displays the current location

Source: Internet
Author: User

1. Add mapkit.framework frame, add field in Plist, to get user's current position setting

Nslocationalwaysusagedescription

2. Code

#import "ViewController.h"

#import <MapKit/MapKit.h>

@interface Viewcontroller () <mkmapviewdelegate>

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

Location Information

@property(nonatomic,strong)cllocation*location;

geocoding Objects

@property(nonatomic,strong)clgeocoder*geocide;

@property(nonatomic,strong)Cllocationmanager*manager;

@end

@implementation Viewcontroller

-(void) viewdidload {

[Super viewdidload];

// type of map display

Mkmaptypestandard: Standard type

Mkmaptypesatellite: satellite model

Mkmaptypehybrid: Mixed model

self. Mapkit. Maptype=mkmaptypestandard;

Tracking user locations in iOS8

if([[uidevice currentdevice]. Systemversion doublevalue]>8.0)

{

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

[self. Manager requestalwaysauthorization];

}

// settings do not allow map rotation

self. Mapkit. rotateenabled=NO;

self. Mapkit. Delegate=self;

// set mapkit How to get the user's location

Mkusertrackingmodenone not tracking

MKUSERTRACKINGMODEFOLLOW Tracking

Mkusertrackingmodefollowwithheading track and get the user's direction

self. Mapkit. Usertrackingmode=mkusertrackingmodefollowwithheading;

}

-(clgeocoder*) geocide

{

if (_geocide= =Nil) {

_geocide=[[clgeocoder alloc]init];

}

return _geocide;

}

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

{

// use geocoding to get location , set title

[self. Geocide reversegeocodelocation: userlocation. Location completionhandler: ^ (nsarray *placemarks, nserror *error) {

clplacemark *mark=[placemarks firstobject];

Userlocation. title=mark. Name;

Userlocation. subtitle=mark. Locality;

}];

// Use the map's current location settings to display the area

cllocationcoordinate2d center=userlocation. Location. coordinate;

// Specify the latitude span

mkcoordinatespan span=mkcoordinatespanmake(0.009310,0.007812);

// Specify the display area in the

mkcoordinateregion region=mkcoordinateregionmake(center, span);

// Set display area

[self . Mapkit setregion: region];

}

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

{

// map Area change complete

NSLog(@ "%f,%f",self. Mapkit. Region. Span. Latitudedelta,self. Mapkit. Region. Span. Longitudedelta);

}

@end

The use of Mapkit displays the current location

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.