In iOS, you can simply use the Mapkit framework for the development of your map.
Basic steps:
- Import Mapkit
- Viewcontroller Inheritance Mkmapviewdelegate Protocol
- Add a Mapkit View
- Prepare a corresponding region information, which is the center, how many radius
- Set the region in Mapview to
- Add a geo-location callout annotation
- 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