Mapkit---The map frame in iOS

Source: Internet
Author: User

In iOS, you can simply use the Mapkit framework for the development of your map.

Basic steps:
    1. Import Mapkit
    2. Viewcontroller Inheritance Mkmapviewdelegate Protocol
    3. Add a Mapkit View
    4. Prepare a corresponding region information, which is the center, how many radius
    5. Set the region in Mapview to
    6. Add a geo-location callout annotation
    7. The location callout is added to the map in the appropriate action.
Viewcontroller
ImportUIKitImportMapkitclassViewcontroller:uiviewcontroller, Mkmapviewdelegate {@IBOutletWeak Var mapview:mkmapview!OverrideFunc Viewdidload () {Super. Viewdidload ()additional setup after loading the view, typically from a nib.Mapview.Delegate= Self///Mkmapview has a delegate attribute, Viewcontroller inherits Mkmapviewdelegate protocol, it must implement the required method in the protocol.Let location = cllocationcoordinate2d (Latitude:22.284681, Longitude:114.158177) Let span = Mkcoordinatespanmake (0.05,0.05)//region can be regarded as location-centric, RADIUS rangeLet region = Mkcoordinateregion (center:location, Span:span)//Mapview will show map of the region        //Mapview.maptype = Mkmaptype.standardMapview.setregion (Region, animated:true)    }OverrideFunc didreceivememorywarning () {Super. Didreceivememorywarning ()//Dispose of any resources, can be recreated.}}

Viewcontroller as shown above, simply prepare the location and then simply mapview the setregion operation to render the map.

Location Callout Annotation

In the map, we can add a callout annotation at the specified location.

Add a location callout on the map let annotation = mkpointannotation () annotation. Coordinate= Location annotation. Title="Hong Kong"Annotation. Subtitle="Someplace"Mapview. Addannotation(annotation)//Add another location callout on the map let Location2 = cllocationcoordinate2d (Latitude:22.294681, Longitude:114.170177) Let annotation2:mkpointannotation = Mkpointannotation () annotation2. Coordinate= Location2 Annotation2. Title="Hong Kong"Annotation2. Subtitle="Someplace2"Mapview. Addannotation(Annotation2)

Effect:

Annotation 1 Annotation 2
Annotation the action when the display is displayed

Sometimes we want the map to appear annotation when you do something, such as automatically dropping or shrinking.

    ///When rendering the annotation, call the methodFunc Mapview (mapview:mkmapview!, didaddannotationviews views: [anyobject]!) {println ("Didaddannotationviews") LetAnnotationview:mkannotationview = views[0] As! Mkannotationview Letannotation = Annotationview.annotation LetRegion:mkcoordinateregion = Mkcoordinateregionmakewithdistance (Annotation.coordinate, -, -) Self. mapview.centercoordinate = Region.center Self. Mapview.setregion (Region, animated:true) Self. Mapview.selectannotation (annotation, animated:true)    }}

For example, once the annotation is present in the map, it automatically jumps to the center of the region, specifying an area of the range.

In addition, the next article prepares to learn about the development of the German map.

Mapkit---The map frame in iOS

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.