iOS development Those things-IOS6 Apple Maps useful to develop

Source: Internet
Author: User
Tags call back

After iOS 6, no longer using Google Maps, but using Apple's own maps, but the API programming interface does not change much. It's a responsible way for developers to develop map applications without having to learn a lot of new stuff. So the same content described in this section applies to the development of map apps on iOS5.

The Map Kit API is used in the iOS app to develop the maps application. Its core is the use of the Mkmapview class. We can set the map display mode, control the map, can add labels on the map.

Show Map

The view that displays the map in the map Kit API is Mkmapview, and its trust agreement is mkmapviewdelegate. The Map Kit API uses the need to import the Mapkit framework.

Below we pass a case introduction below we introduce the use of the map Kit API. This case in the "Input query location keyword" text box enter keyword, click on the "Query" button, the first geographical information coding query, query to obtain landmark information, marked on the map.

First join the frame mapkit.framework. Then open the Mainstoryboard.storyboard IB design in Project and drag the map view from the object library to the design screen.

Adjust its position and size so that map view fills in the blanks below the screen as much as possible, and then defines the input exit for the map view. Let's look at the main view controller ViewController.h code:

#import <UIKit/UIKit.h> #import <MapKit/MapKit.h> #import "MapLocation.h" @interface Viewcontroller: Uiviewcontroller <MKMapViewDelegate> @property (weak, nonatomic) Iboutlet Uitextfield *txtquerykey; @property (Weak, nonatomic) Iboutlet Mkmapview *mapview; -(Ibaction) Geocodequery: (ID) sender; @end


Since the use of the map Kit API requires the introduction of header file <mapkit/mapkit.h>, the header file "MapLocation.h" is our custom descriptive narrative map labeling point class. When defining Viewcontroller, you also need to declare the implementation of the Mkmapviewdelegate protocol. The Txtquerykey property is the Query keyword text box, and the Mapview property is the Mkmapview type, which corresponds to the screen. Click the Query button to trigger the Geocodequery: method, which processes the query and makes annotations on the map.

Let me take a look at VIEWCONTROLLER.M's Viewdidload method code:

-(void) Viewdidload{[super Viewdidload];_mapview.maptype = Mkmaptypestandard;_mapview.delegate = self;}


Set the type of the map in the Viewdidload method, which has 3 types:

Mkmaptypestandard Callout Map type.

Mkmaptypesatellite satellite map type. There are no information such as street names in satellite maps;

Mkmaptypehybrid mixed map types. In a hybrid map, the street information is marked on a satellite map;

The _mapview.delegate = Self Statement of the Viewdidload method is the delegate property that the current view controller assigns to the map view, so that the map view will call back Viewcontroller when needed, assuming that it fails, Callback the following failure method:

-(void) Mapviewdidfailloadingmap: (Mkmapview *) Themapview witherror: (nserror *) error {NSLog (@ "error:%@", [Error Description]);}


Track User Location changes

Mapkit provides an API to track changes in user location and direction, and we do not have to write the location service code ourselves. Open the Showsuserlocation property of the map view and set the method Setusertrackingmode: You can, code such as the following:

-(void) Viewdidload{[super viewdidload];if ([Cllocationmanager locationservicesenabled]) {_mapview.maptype = Mkmaptypestandard;_mapview.delegate = Self;_mapview.showsuserlocation = YES; [_mapview Setusertrackingmode:mkusertrackingmodefollow animated:yes];}


The Code _mapview.showsuserlocation = YES, agreeing to track display of user location information. Displaying the user location on an iOS device is a shiny blue dot.

[_mapview Setusertrackingmode:mkusertrackingmodefollow Animated:yes] Statement set user tracking mode, there are 3 types of user tracking mode:

Mkusertrackingmodenone. No real user tracking mode;

Mkusertrackingmodefollow. Ability to track changes in user location;

Mkusertrackingmodefollowwithheading. Ability to track changes in user position and direction;

Then, we also need to implement the map view entrusted method Mapview:didupdateuserlocation:, its code such as the following:

-(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation{_ Mapview.centercoordinate = userLocation.location.coordinate;}


This method is callback when the location service is updated, and we adjust the center point of the map again as the center point of the current user in this method, in which _mapview.centercoordinate = The UserLocation.location.coordinate statement is for that purpose.

These lines of code will be able to track changes in the user's location, but for such a small case it is not necessary to take the iphone and run around the outside for a test. With the ability to test with the simulator, the simulator has several fixed patterns that can emit continuously changing data. Test execution There will be a small circle in the screen, which is executed at the speed we set.

iOS development Those things-IOS6 Apple Maps useful to develop

Related Article

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.