The development of the German map needs to be http://lbs.amap.com/console/to register an AK, create a new Swift project, and then add a info.plist Nslocationalwaysusagedescription or nslocationwheninuseusagedescription.
A library of gold maps and a dependency library added to the project
The required libraries are as follows:
Add header file
See Swift basics in a specific way-call a third-party OC Project, add the following code to the Bridging-header.h, so we can invoke the map-related interfaces
#import <MAMapKit/MAMapKit.h> #import <AMapSearchKit/AMapSearchAPI.h>
Basic Map class implementation
The basic class is composed of map related initialization and function development, the interface is as follows: include positioning, data refresh, zoom in and out add, and function implementation.
basemapcontroller.swift//swiftmap//Map Base section//Created by System Administrator on 15/1/24.//Copyright (c) 2015 Jwzhangjie. All rights Reserved.//import Uikitclass Basemapcontroller:uiviewcontroller, mamapviewdelegate, AMapSearchDelegate { var mapview:mamapview! var search:amapsearchapi! var centercoordinate:cllocationcoordinate2d! var centermarker:uiimageview! Func Initmapview () {Mapview = Mamapview (Frame:cgrectmake (0, +, Cgrectgetwidth (self.view.bounds), Cgrectgetheight ( Self.view.bounds) ( -65)) Mapview.showsuserlocation = True Mapview.setusertrackingmode (mausertrackingmodefollow , animated:true) Mapview.showscompass = False Mapview.showsscale = True Mapview.scaleorigin = Cgpoin Tmake (mapview.frame.size.height-20) mapview.delegate = self Self.view.addSubview (mapview)} f UNC Initsearchview () {search = Amapsearchapi (SearchKey:MAMapServices.sharedServices (). ApiKey, Delegate:self)} func Initbtns () {centermarker = Uiimageview (frame:cgrectmake (0, 0,.)) Centermar Ker.center = Mapview.center Centermarker.frame=cgrectmake (centermarker.frame.origin.x, CENTERMARKER.FRAME.ORIGIN.Y -65, 38, 50); Centermarker.image = UIImage (named: "Green_pin.png") Mapview.addsubview (Centermarker)//Position button var Loca Tionbtn:uibutton = UIButton (Frame:cgrectmake (mapview.frame.size.height-70, +)) Locationbtn.setbackgroundi Mage (UIImage (named: "Ic_locate.png"), ForState:UIControlState.Normal) locationbtn.setbackgroundimage (UIImage (name D: "Ic_locate_press.png"), forState:UIControlState.Selected) Locationbtn.setbackgroundimage (UIImage (named: "Ic_loc Ate_press.png "), forState:UIControlState.Highlighted) Locationbtn.tag = 1; Locationbtn.addtarget (Self, Action: "Btnselector:", ForControlEvents:UIControlEvents.TouchUpInside) mapview.addsub View (LOCATIONBTN)//Refresh button var Refreshbtn:uibutton = UIButton (Frame:cgrectmake (mapview.frame.size.height-110, +)) Refreshbtn.setbac Kgroundimage (UIImage (named: "Ic_refresh.png"), ForState:UIControlState.Normal) Refreshbtn.setbackgroundimage (uiim Age (named: "Ic_refresh_press.png"), forState:UIControlState.Selected) Refreshbtn.setbackgroundimage (UIImage (named : "Ic_refresh_press.png"), forState:UIControlState.Highlighted) Refreshbtn.tag = 2; Refreshbtn.addtarget (Self, Action: "Btnselector:", ForControlEvents:UIControlEvents.TouchUpInside) MAPVIEW.ADDSUBV Iew (REFRESHBTN)//Zoom Out button var Zoomoutbtn:uibutton = UIButton (Frame:cgrectmake (mapview.frame.size.width-15-35 , mapview.frame.size.height-70, (+)) Zoomoutbtn.setbackgroundimage (UIImage (named: "Ic_zoom_out.png"), Forstate: Uicontrolstate.normal) Zoomoutbtn.setbackgroundimage (UIImage (named: "Ic_zoom_out_press.png"), Forstate:uicontrols Tate. Selected) Zoomoutbtn.setbackgroundimaGE (UIImage (named: "Ic_zoom_out_press.png"), forState:UIControlState.Highlighted) Zoomoutbtn.tag = 3; Zoomoutbtn.addtarget (Self, Action: "Btnselector:", ForControlEvents:UIControlEvents.TouchUpInside) MAPVIEW.ADDSUBV Iew (ZOOMOUTBTN)//magnify button var Zoominbtn:uibutton = UIButton (Frame:cgrectmake (mapview.frame.size.width-15-35, M apview.frame.size.height-110, Zoominbtn.setbackgroundimage (UIImage (named: "Ic_zoom_in.png"), Forstate:uic Ontrolstate.normal) Zoominbtn.setbackgroundimage (UIImage (named: "Ic_zoom_in_press.png"), Forstate:uicontrolstate. Selected) Zoominbtn.setbackgroundimage (UIImage (named: "Ic_zoom_in_press.png"), ForState:UIControlState.Highlighte d) Zoominbtn.tag = 4; Zoominbtn.addtarget (Self, Action: "Btnselector:", ForControlEvents:UIControlEvents.TouchUpInside) Mapview.addsubvi EW (ZOOMINBTN)} func btnselector (Sender:uibutton) {switch Sender.tag {case 1://location If centercoordinate! = nil {mapview.setcentercoordinate (centercoordinate, Animated:true) } Case 2://Refresh Getlocationroundflag () Mapview.showsuserlocation = true; YES to open position, NO for off position case 3:if Mapview.zoomlevel >= 4 && mapview.zoomlevel <= 19{ Mapview.setzoomlevel (MAPVIEW.ZOOMLEVEL-1, animated:true)}else if Mapview.zoomlevel >= 3 && Mapview.zoomlevel < 4{mapview.setzoomlevel (3, Animated:true)} case 4:i F mapview.zoomlevel >= 3 && mapview.zoomlevel <= 18{mapview.setzoomlevel (mapview.zoomlevel+1 , animated:true)}else if mapview.zoomlevel > && mapview.zoomlevel <= 19{MAPV Iew.setzoomlevel (Animated:true)} default:println ("not Known")}} Fu NC getlocationroundflag () {} func MApview (mapview:mamapview!, didupdateuserlocation userlocation:mauserlocation!, Updatinglocation:bool) {if updat Inglocation {//Take out the coordinates of the current position println ("Latitude:%f,longitude:%f", Userlocation.coordinate.latitude,use RLocation.coordinate.longitude); Centercoordinate = Cllocationcoordinate2dmake (userlocation.coordinate.latitude,userlocation.coordinate.longitude ); Mapview.showsuserlocation = false; }}//Clear Data func Clearmapdata () {Clearmapview () ClearSearch ()} func Clearmapview () { Mapview.showsuserlocation = False mapview.delegate = nil} func ClearSearch () {Self.search.d Elegate = nil} override func didreceivememorywarning () {super.didreceivememorywarning ()//DISPO SE of any resources the can be recreated. }}
Concrete Implementation Class Detailviewcontroller Inheritance Basemapcontroller
detailviewcontroller.swift//swiftmap////Created by System Administrator on 15/1/22.//Copyright (c) 2015 Jwzha Ngjie. All rights Reserved.//import Uikitclass Detailviewcontroller:basemapcontroller {var data:nsmutabledata! @IBAction func returnhome (sender:anyobject) {Mapview.removefromsuperview () Self.navigationcontroller?. Popviewcontrolleranimated (True)} @IBAction func segmentchanged (sender:anyobject) {switch sender.select edsegmentindex{Case 0://Certified vehicle println ("0") Case 1://All Vehicles println ("1") Default: println ("Default")}} override Func Viewdidload () {super.viewdidload () Initma PView () Initsearchview () initbtns () data = Nsmutabledata (); } override func Getlocationroundflag () {var requesturl:string = "http://api.map.baidu.com/geosearch/v3/n earby?ak=dczobrbgddd2s4qlcec4yvof&geotable_id=92326&Location=121.613461,31.197495&radius=1000000&sortby=distance:1 ";//var request:nsurlrequest = NSURLRequest (Url:nsurl (String:requesturl))//var connect:nsurlconnection = nsurlconnection (request:request, delegate:self)!/ /data = Nsmutabledata () println (Requesturl)} override func didreceivememorywarning () { Super.didreceivememorywarning ()//Dispose of any of the resources that can is recreated. }}
Remember to test on the real machine.
Swift Foundation--Gold Map Practice (i)