IOS development-iOS 6 practical development of Apple Maps

Source: Internet
Author: User

After iOS 6, Google map is no longer used, but Apple's own map is used, but the API programming interface does not change much. Developers can develop map applications without learning many new things. Therefore, this section also applies to running map application development on iOS5.

 

Use Map Kit API in iOS applications to develop Map applications. Its core is the MKMapView class. You can set the Map Display Mode and control the map. You can add annotations to the map.

Show Map

In the Map Kit API, the Map view is MKMapView, and its delegate protocol is MKMapViewDelegate. The Map Kit API needs to be imported into the MapKit framework.

Next we will introduce the use of Map Kit API through a case. In this case, enter a keyword in the "Enter query location keyword" text box and click the "query" button to perform the Geographic Information encoding query first. After obtaining the landmark information, mark it on the map.

First, add the framework MapKit. framework. Then open the IB design of MainStoryboard. storyboard in the project, drag the Map View from the object library to the design screen.

Adjust its position and size so that Map View can fill in the blank part of the screen as much as possible, and then define the input exit for Map View. Let's take a look at the main View Controller ViewController. h code:

 <UIKit/UIKit.h> <MapKit/MapKit.h> ViewController : UIViewController <MKMapViewDelegate>**- (IBAction)geocodeQuery:(

 

When using the Map Kit API, we need to introduce the header file <MapKit/MapKit. h>. The header file "MapLocation. h" is our own description Map annotation Point class. When defining ViewController, you also need to declare the implementation of the MKMapViewDelegate protocol. The txtQueryKey attribute is the text box for querying the off key, and the mapView attribute is of the MKMapView type, which corresponds to the screen. Click the query button to trigger geocodeQuery: method, which processes the query and performs the Annotation on the map.

Next let's take a look at the viewDidLoad method code of ViewController. m:

- (= =

 

Set the map type in the viewDidLoad method. There are three types:

MKMapTypeStandard indicates the map type.

MKMapTypeSatellite satellite map type. No street name or other information in the satellite map;

MKMapTypeHybrid hybrid map type. A hybrid map is used to mark sub-district information on a satellite map;

The _ mapView of the viewDidLoad method. the delegate = self statement is the delegate attribute assigned by the current view controller to the map view. As a result, the map view calls back the ViewController when needed. If the view fails, the following failure method is called back:

- ()mapViewDidFailLoadingMap:(MKMapView *)theMapView withError:(NSError *%

 

 

Tracking user location changes

MapKit provides an API to track user location and direction changes. You do not need to write your own service code. Enable the showsUserLocation attribute of the map view and set the setUserTrackingMode method. The Code is as follows:

- (= ==

 

 

Code _ mapView. showsUserLocation = YES allows tracking to display user location information. On iOS devices, the user location is displayed as a bright blue dot.

[_ MapView setUserTrackingMode: MKUserTrackingModeFollow animated: YES] The statement sets the User tracking mode. There are three User tracking modes:

MKUserTrackingModeNone. No user tracking mode;

MKUserTrackingModeFollow. Tracking user location changes;

MKUserTrackingModeFollowWithHeading. Tracking user location and direction changes;

Then, we also need to implement the map view delegate method mapView: didUpdateUserLocation:. Its code is as follows:

- ()mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *=

 

 

This delegate method calls back this method when locating the service update completion user location. In this method, we re-adjust the center of the map to the center of the current user, where _ mapView. centerCoordinate = userLocation. location. the coordinate statement is used for this purpose.

These lines of code can be used to track user location changes, but it is not necessary to take the iPhone and run a circle outside for testing in such a small case. The simulator can be used for testing. The simulator has several fixed modes to send continuously changed data. The test runs in a small circle on the screen, which keeps running at our set speed.

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.