Swift basics: no foundation for swift
AMAP developers need to register an ak on the official website http://lbs.amap.com/console/, create a swift project, and then add an NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription in Info. plist.
Add the library and dependent library of AMAP to the project.
The required libraries are as follows:
Add header file
For details about the method, refer to the basic Swift-call the third-party OC project, add the following code in the Bridging-Header.h, so that we can call the AMAP-related interface
#import <MAMapKit/MAMapKit.h>#import <AMapSearchKit/AMapSearchAPI.h>
Basic Map implementation
Write map-related initialization and function development in the basic class. The interface is as follows: including positioning, data refresh, zoom-in, zoom-in, and add functions.
//// BaseMapController. swift // SwiftMap // basic part of the map // Created by System Administrator on 15/1/24. // Copyright (c) 2015 Zhangjie. 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, 65, 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 = CGPointMake (100, mapView. frame. size. height-20) mapView. delegate = self. view. a DdSubview (mapView)} func initSearchView () {search = AMapSearchAPI (searchKey: MAMapServices. sharedServices (). apiKey, delegate: self)} func initBtns () {centerMarker = UIImageView (frame: CGRectMake (0, 0, 38, 50) centerMarker. 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) // locate the button var locationBtn: UIButton = UIButton (frame: CGRectMake (15, mapView. frame. size. height-70, 35, 35) locationBtn. setBackgroundImage (UIImage (named: "ic_locate.png"), forState: UIControlState. normal) locationBtn. setBackgroundImage (UIImage (named: "ic_locate_press.png"), forState: UIControlState. selected) locationBtn. setBackgroundImage (UIImage (named: "ic_locate_pr Ess.png "), forState: UIControlState. highlighted) locationBtn. tag = 1; locationBtn. addTarget (self, action: "btnSelector:", forControlEvents: UIControlEvents. touchUpInside) mapView. addSubview (locationBtn) // refresh button var refreshBtn: UIButton = UIButton (frame: CGRectMake (15, mapView. frame. size. height-110, 35, 35) refreshBtn. setBackgroundImage (UIImage (named: "ic_refresh.png"), forState: UIControlState. norma L) refreshBtn. setBackgroundImage (UIImage (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. addSubview (refreshBtn) // zoom out button var zoomOutBtn: UIButton = UIButton (frame: CGRectMake (mapView. frame. size. width-15-35, mapView. frame. size. height-70, 35, 35) zoomOutBtn. setBackgroundImage (UIImage (named: "ic_zoom_out.png"), forState: UIControlState. normal) zoomOutBtn. setBackgroundImage (UIImage (named: "ic_zoom_out_press.png"), forState: UIControlState. 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. addSubview (zoomOutBtn) // zoom in button var zoomInBtn: UIButton = UIButton (frame: CGRectMake (mapView. frame. size. width-15-35, mapView. frame. size. height-110, 35, 35) zoomInBtn. setBackgroundImage (UIImage (named: "ic_zoom_in.png"), forState: UIControlState. normal) zoomInBtn. SetBackgroundImage (UIImage (named: "ic_zoom_in_press.png"), forState: UIControlState. selected) zoomInBtn. setBackgroundImage (UIImage (named: "ic_zoom_in_press.png"), forState: UIControlState. highlighted) zoomInBtn. tag = 4; zoomInBtn. addTarget (self, action: "btnSelector:", forControlEvents: UIControlEvents. touchUpInside) mapView. addSubview (zoomInBtn)} func btnSelector (sender: UIButton) {switch send Er. tag {case 1: // locate if centerCoordinate! = Nil {mapView. setCenterCoordinate (centerCoordinate, animated: true)} case 2: // refresh getLocationRoundFlag () mapView. showsUserLocation = true; // YES indicates that the location is enabled, and NO indicates that the location is disabled. case 3: if mapView. zoomLevel> = 4 & amp; 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: if mapView. ZoomLevel> = 3 & mapView. zoomLevel <= 18 {mapView. setZoomLevel (mapView. zoomLevel + 1, animated: true)} else if mapView. zoomLevel> 18 & mapView. zoomLevel <= 19 {mapView. setZoomLevel (19, animated: true)} default: println ("not known")} func getLocationRoundFlag () {} func mapView (mapView: MAMapView !, DidUpdateUserLocation userLocation: MAUserLocation !, UpdatingLocation: Bool) {if updatingLocation {// retrieve the coordinate println ("latitude: % f, longpolling: % f", userLocation. coordinate. latitude, userLocation. coordinate. longpolling); centerCoordinate = CLLocationCoordinate2DMake (userLocation. coordinate. latitude, userLocation. coordinate. longpolling); mapView. showsUserLocation = false ;}}// clear data func clearMapData () {clearMapView () clearSearch ()} func clearMapView () {mapView. showsUserLocation = false mapView. delegate = nil} func clearSearch () {self. search. delegate = nil} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}
The specific implementation class DetailViewController inherits BaseMapController
/// DetailViewController. swift // SwiftMap /// Created by System Administrator on 15/1/22. // Copyright (c) 2015 Zhangjie. 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. selectedSegmentIndex {case 0: // certified vehicle println ("0") case 1: // all vehicles println ("1") default: println ("default ")}} override func viewDidLoad () {super. viewDidLoad () initMapView () initSearchView () initBtns () data = NSMutableData ();} override func getLocationRoundFlag () {var requestUrl: String = "http://api.m Ap.baidu.com/geosearch/v3/nearby? 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 resources that can be recreated .}}
Remember to test on a real machine