iOS development-Easy to use with Apple's own map

Source: Internet
Author: User

"Implement Features":

1. Show a simple map

2. Display the user's current location

3. Control the View size

4. PIN Click to display information

"Implementation Steps"

1. Import two required frames: Corelocation.framework and Mapkit.framework

2. Introduce the header file:

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

3. A variable of two classes is required, defined here as a property:

@property (nonatomic, retain) Mkmapview *mapview;
@property (nonatomic, retain) Cllocationmanager *locamanager;

4. Don't forget to accept the agreement

@interface Viewcontroller () <cllocationmanagerdelegate,mkmapviewdelegate>

5.OK Start the Code

1- (void) viewdidload{2 [Super Viewdidload];3     4     //Initialize location Management5Self.locamanager =[[Cllocationmanager alloc] init];6     //if the device is in iOS8 and above, you have to give the user authorization before you can get the location permission .7     if([[Uidevice currentdevice].systemversion Doublevalue] >=8.0) {8 [Self.locamanager requestwheninuseauthorization];9     }Ten     //Set up proxy OneSelf.locamanager.Delegate=Self ; A     //Start Positioning - [Self.locamanager startupdatinglocation]; -      the      -     //Initialize the map view -Self.mapview =[[Mkmapview alloc] initWithFrame:self.view.frame]; -     //set map style to basic style (there are three styles here) +Self.mapView.mapType =Mkmaptypestandard; -     //whether to display the current location +Self.mapView.showsUserLocation =YES; A     //Set up proxy atSelf.mapview.Delegate=Self ; - [Self.view AddSubview:self.mapView]; -}

Implementation of Proxy method

#pragmaMark--mapviewdelegate-(void) Mapviewdidfinishloadingmap: (Mkmapview *) mapview{NSLog (@"Load Complete");}- (void) Mapviewwillstartlocatinguser: (Mkmapview *) mapview{NSLog (@"going to get user location");}- (void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{//Click on the PIN to show the messageUserlocation.title =@"James Ding"; Userlocation.subtitle=@"Dingjianjaja"; //To move the map view to the user's current location[Mapview setCenterCoordinate:userLocation.location.coordinate Animated:yes]; //set the accuracy and display the user's locationMkcoordinatespan span = Mkcoordinatespanmake (1,1);//scale of 1:10^5,1 cm stands for 1 kmMkcoordinateregion region =Mkcoordinateregionmake (userLocation.location.coordinate, span); [Mapview setregion:region animated:yes];}

6. Running

The discovery does not show the user's location, because the program uses the three status after IOS8, that is, the trial period/always/never, so we need to configure in the Plist file: Trial period or always

Nslocationwheninuseusagedescription

The simple display map has its own location, and the following will continue to join the destination PIN, as well as connect the current location and destination, and calculate the line distance function.

iOS development-Easy to use with Apple's own map

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.