IOS learning notes -- MapKit

Source: Internet
Author: User

IOS learning notes -- MapKit

In iOS applications, we can use the Map Kit API to develop Map applications. Its core is the MKMapView class. The main function is to display maps, add tags, and track user location changes.

(All the content of this log is based on the coordinate position that has been obtained successfully. For details about the method, see the previous log)

1. display a map

First, you must reference Its principal protocol is MKMapViewDelegate.

Initialize MapView

mapView = [[MKMapView alloc] initWithFrame:CGRectMake(5, 120, self.view.frame.size.width - 10, 430)];    mapView.mapType = MKMapTypeStandard;    mapView.delegate = self;

There are three map types:

MKMapTypeStandard annotation map type

MKMapSatellite satellite map type

MKMapTypeHybrid hybrid map type


After obtaining the placemark of the location, I customized a function to display the map.

-(Void) showInMapView: (CLPlacemark *) placemark {CLLocationCoordinate2D coordinate = placemark. location. coordinate; // Add MapView MKCoordinateSpan span = MKCoordinateSpanMake (0.01, 0.01); // span (proportion) MKCoordinateRegion region = MKCoordinateRegionMake (coordinate, span ); // range and region [mapView setRegion: region]; [self. view addSubview: mapView];}
You only need to set the display area of the mapView, and use the following struct:

Typedef struct {

CLLocationCoordinate2D center; // center point

MKCoordinateSpan Span; // Span

} MKCoodinateRegion;

The MKCoodinateRegionMake function is used for struct initialization. The first parameter specifies the center point of the target region and the second parameter specifies the span of the target region.

The MKCoordinateSpan struct of the display span is defined:

Typedef struct {

CLLocationDegress latitudeDelta; // North-South span of the Region

CLLocationDegress longtitudeDelta; // the east-west span of the region.

} MKCoordinateSpan;

The north-south span is about 111 kilometers, and the east-west span is about 111 kilometers on the equator. The closer the east-west span is to the Poles, the shorter the distance is, and the change to 0 kilometers at the pole.

Finally, use the setRegion function to set the display area for mapView.

2. Add Annotation

First, you must reference its header file. ,

First, set the annotation point. The MKPointAnnotation class is required. It mainly has the following three attributes:

-(NSString *) titile; the title of the vertex

-(NSString *) subtitle; subtitle of the annotation point

-(CLLocationCoordinate2D) coordinate; // location information of the annotation point

After the preceding three attributes are set, use the addAnnotation function to add the annotation points to the mapView.

The code is still in showInMapView: In the function, the Code is as follows:

-(Void) showInMapView: (CLPlacemark *) placemark {CLLocationCoordinate2D coordinate = placemark. location. coordinate; // Add MapView MKCoordinateSpan span = MKCoordinateSpanMake (0.01, 0.01); region = compute (coordinate, span); [mapView setRegion: region animated: YES]; [self. view addSubview: mapView]; // Add Annotation MKPointAnnotation * annotaion = [[MKPointAnnotation alloc] init]; annotaion. coordinate = coordinate; annotaion. title = placemark. locality; annotaion. subtitle = placemark. name; [mapView addAnnotation: annotaion];}

To display the annotation, we should also implement the mapView: viewForAnnotation: Method in the map view delegate Protocol. In this method, we instantiate a MKPointAnnotationView ), and complete its initialization. The implementation code is as follows:

-(MKAnnotationView *) mapView :( MKMapView *) mapView viewForAnnotation :( id
 
  
) Annotation {comment * annotaionView = (MKPinAnnotationView *) [mapView comment: @ "PIN_ANNOTATION"]; if (annotaionView = nil) {annotaionView = [[MKPinAnnotationView alloc] Comment: annotation reuseIdentifier: @ "PIN_ANNOTATION"];} annotaionView. pinColor = MKPinAnnotationColorRed; // annotaionView. animatesDrop = YES; // animated annotaionView. canShowCallout = YES; // illustration No. return annotaionView ;}
 
(MBAnnotationView *)-dequeueReusableAnnotationViewWithIdentifier: returns reusable annotation views using identifiers.
3. tracking user location changes

Enable the showsUserLocation attribute of the map and set the setUserTrackMode method to track the location and direction changes of the user.

    mapView.showsUserLocation = YES;    [mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:YES];
There are three User tracking modes:

MKUserTrackingModeNone has no user tracking mode

MKUsetTrackingModeFollow tracks user location changes
MKUserTrackingModeFollowWithHeading tracks user location and direction changes

At the same time, you also need to implement the map view delegate method mapView: didUpdataUserLocation:

- (void) mapView:(MKMapView *) mapView didUpdateUserLocation:(MKUserLocation *)userLocation{    mapView.centerCoordinate = userLocation.location.coordinate;}









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.