iOS development Those things-IOS6 Apple Maps practical development

Source: Internet
Author: User
Tags call back uikit

After iOS 6, Google Maps was no longer used, but Apple's own maps were used, but the API programming interface didn't change much. It is a responsible practice for developers to develop map applications without having to learn a lot of new things. So this section also applies to running Map app development 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, you can add labels on the map.

Show Map

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

Let's take a look at a case study. Let's introduce the use of the map Kit API. This case in the "Input query location keyword" text box to enter keywords, click on the "Query" button, the first geographical information coding query, query to obtain landmark information, marked on the map.

First add the frame mapkit.framework. Then open the Mainstoryboard.storyboard IB design in the 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:

[CPP]View Plaincopy
  1. #import <UIKit/UIKit.h>
  2. #import <MapKit/MapKit.h>
  3. #import "MapLocation.h"
  4. @interface Viewcontroller:uiviewcontroller <MKMapViewDelegate>
  5. @property (Weak, nonatomic) Iboutlet Uitextfield *txtquerykey;
  6. @property (Weak, nonatomic) Iboutlet Mkmapview *mapview;
  7. -(Ibaction) Geocodequery: (ID) sender;
  8. @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 own definition of the description map callout Point class. When defining Viewcontroller, it is also necessary 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:

[CPP]View Plaincopy
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. _mapview.maptype = Mkmaptypestandard;
    5. _mapview.delegate = self;
    6. }


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, and if it fails, Callback the following failure method:

[CPP]View Plaincopy
    1. -(void) Mapviewdidfailloadingmap: (Mkmapview *) Themapview witherror: (nserror *) Error {
    2. NSLog (@ "error:%@", [error description]);
    3. }


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: Yes, the code is as follows:

[CPP]View Plaincopy
  1. -(void) Viewdidload
  2. {
  3. [Super Viewdidload];
  4. if ([Cllocationmanager locationservicesenabled])
  5. {
  6. _mapview.maptype = Mkmaptypestandard;
  7. _mapview.delegate = self;
  8. _mapview.showsuserlocation = YES;
  9. [_mapview Setusertrackingmode:mkusertrackingmodefollow Animated:yes];
  10. }
  11. }


where code _mapview.showsuserlocation = YES allows tracing to display 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 user tracking mode;

Mkusertrackingmodefollow. Can track the user's location change;

Mkusertrackingmodefollowwithheading. Can track the user's position and direction change;

Then, we also need to implement the Map view delegate method Mapview:didupdateuserlocation:, its code is as follows:

[CPP]View Plaincopy
    1. -(void) Mapview: (Mkmapview *) Mapview didupdateuserlocation: (mkuserlocation *) userlocation
    2. {
    3. _mapview.centercoordinate = userLocation.location.coordinate;
    4. }


The delegate method callback the method when the location service update completes the user's position, and in this method we readjust the center point of the map to the center point of the current user, where _mapview.centercoordinate = The UserLocation.location.coordinate statement is for that purpose.

These lines of code can 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. Simulator testing can be used, and the simulator has several fixed patterns that can emit continuously changing data. The test run will have a small circle in the screen that runs as fast as we set it.

iOS development Those things-IOS6 Apple Maps practical development

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.