MKMapView learning notes

Source: Internet
Author: User
I. Basic knowledge most mainstream smartphones currently support GoogleMap, and map programs on mobile phones can indeed bring great convenience to our travel. Using the MapKit framework on the iPhone can easily display Google Maps and add tags on maps.

Ii. Details

1. MKMapView display

(1) Create MKMapView

CGRect rect = CGRectMake(0, 20, 320, 460);MKMapView *mapView = [[MKMapView alloc] initWithFrame:rect];
(2) set the longitude and latitude
CLLocationCoordinate2D theCoordinate;theCoordinate.latitude=24.148926;theCoordinate.longitude=120.715542;
(3) set the display range
MKCoordinateSpan theSpan;theSpan.latitudeDelta=0.1;theSpan.longitudeDelta=0.1;
(4) set the center and range of Map Display
MKCoordinateRegion theRegion;theRegion.center=theCoordinate;theRegion.span=theSpan;
(5) set the map display type and display by range
[mapView setMapType:MKMapTypeStandard];[mapView setRegion:theRegion];
After completing these steps, you can add mapView to the current view.

2. add annotation (1) and annotation-related classes and protocols (a) MKAnnotation Protocol Annotation on MKMapView. This Protocol must be implemented and has three attributes: coordinate, title, and subtitle, the coordinate attribute must be set. @ Property (nonatomic, readonly) CLLocationCoordinate2D coordinate (B) MKAnnotationView after setting Annotation, you can use this to display the Annotation on the map,

- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
Its important attributes include @ property (nonatomic, retain) UIImage * image, which is a custom image labeled on a map.

@ Property (nonatomic) BOOL canShowCallout setting click to bring up the Annotation

@ Property (retain, nonatomic) UIView * rightCalloutAccessoryViewproperty (retain, nonatomic) UIView * leftCalloutAccessoryView is set to display the attached View after clicking the left and right sides of the annotation.

(C) MKPinAnnotationView

This is a pin display annotation, inherited from MKAnnotationView, and added two attributes @ property (nonatomic) MKPinAnnotationColor pinColor to set the color of the pin, which can be red, green, or purple.

@ Property (nonatomic) BOOL animatesDrop: Specifies whether the PIN is displayed as a falling animation.

(2) Add Annotation on the map step (a) create a class that implements the MKAnnotation protocol, and set its coordinate attribute in the class's initialization function (B) create Annotation (c) using the above method Add the created Annotation to MapView using the addAnnotation method (d) to implement MKMapViewDelegate proxy. In the proxy Function

-(MKAnnotationView *) mapView :( MKMapView *) mView viewForAnnotation :( id <MKAnnotation>) in annotation, the Annotation is marked on the map as MKPinAnnotationView or MKAnnotationView.

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.