Swift基础 - - 高德地图实践

高德地图开发需要自己到官网http://lbs.amap.com/console/ 注册一个ak,新建一个swift工程,然后在Info.plist中添加一个NSLocationAlwaysUsageDescription或者NSLocationWhenInUseUsageDescription。

高德地图的库以及依赖库加入到项目里面

需要的库如下截图:

Swift基础 - - 高德地图实践_第1张图片

添加头文件

具体的方式见Swift基础--调用第三方OC项目,在Bridging-Header.h中加入如下代码,这样我们就可以调用高德地图相关的接口

[objc]  view plain  copy
 print ?
  1. #import <MAMapKit/MAMapKit.h>  
  2. #import <AMapSearchKit/AMapSearchAPI.h>  

基础地图类实现

基础类里面编写地图相关的初始化以及功能的开发,界面如下:包含定位,数据刷新,放大缩小添加以及功能实现。

Swift基础 - - 高德地图实践_第2张图片

[objc]  view plain  copy
 print ?
  1. //  
  2. //  BaseMapController.swift  
  3. //  SwiftMap  
  4. //  地图的基础部分  
  5. //  Created by System Administrator on 15/1/24.  
  6. //  Copyright (c) 2015年 jwzhangjie. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class BaseMapController : UIViewController, MAMapViewDelegate, AMapSearchDelegate {  
  12.       
  13.     var mapView:MAMapView!  
  14.     var search:AMapSearchAPI!  
  15.     var centerCoordinate:CLLocationCoordinate2D!  
  16.       
  17.     var centerMarker:UIImageView!  
  18.       
  19.     func initMapView(){  
  20.         mapView = MAMapView(frame: CGRectMake(065, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-65))  
  21.         mapView.showsUserLocation = true  
  22.         mapView.setUserTrackingMode(MAUserTrackingModeFollow, animatedtrue)  
  23.         mapView.showsCompass = false  
  24.         mapView.showsScale = true  
  25.         mapView.scaleOrigin = CGPointMake(100, mapView.frame.size.height-20)  
  26.         mapView.delegate = self  
  27.         self.view.addSubview(mapView)  
  28.     }  
  29.       
  30.     func initSearchView(){  
  31.         search = AMapSearchAPI(searchKey: MAMapServices.sharedServices().apiKey, delegateself)  
  32.     }  
  33.       
  34.     func initBtns(){  
  35.         centerMarker = UIImageView(frame: CGRectMake(003850))  
  36.         centerMarker.center = mapView.center  
  37.         centerMarker.frame=CGRectMake(centerMarker.frame.origin.x, centerMarker.frame.origin.y-653850);  
  38.         centerMarker.image = UIImage(named: "green_pin.png")  
  39.         mapView.addSubview(centerMarker)  
  40.         //定位按钮  
  41.         var locationBtn:UIButton = UIButton(frame: CGRectMake(15, mapView.frame.size.height-703535))  
  42.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate.png"), forState: UIControlState.Normal)  
  43.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate_press.png"), forState: UIControlState.Selected)  
  44.         locationBtn.setBackgroundImage(UIImage(named: "ic_locate_press.png"), forState: UIControlState.Highlighted)  
  45.         locationBtn.tag = 1;  
  46.         locationBtn.addTarget(self, action:"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  47.         mapView.addSubview(locationBtn)  
  48.         //刷新按钮  
  49.         var refreshBtn:UIButton = UIButton(frame: CGRectMake(15, mapView.frame.size.height-1103535))  
  50.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh.png"), forState: UIControlState.Normal)  
  51.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh_press.png"), forState: UIControlState.Selected)  
  52.         refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh_press.png"), forState: UIControlState.Highlighted)  
  53.         refreshBtn.tag = 2;  
  54.         refreshBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  55.         mapView.addSubview(refreshBtn)  
  56.         //缩小按钮  
  57.         var zoomOutBtn:UIButton = UIButton(frame: CGRectMake(mapView.frame.size.width - 15-35, mapView.frame.size.height-703535))  
  58.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out.png"), forState: UIControlState.Normal)  
  59.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out_press.png"), forState: UIControlState.Selected)  
  60.         zoomOutBtn.setBackgroundImage(UIImage(named: "ic_zoom_out_press.png"), forState: UIControlState.Highlighted)  
  61.         zoomOutBtn.tag = 3;  
  62.         zoomOutBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  63.         mapView.addSubview(zoomOutBtn)  
  64.         //放大按钮  
  65.         var zoomInBtn:UIButton = UIButton(frame: CGRectMake(mapView.frame.size.width-15-35, mapView.frame.size.height-1103535))  
  66.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in.png"), forState: UIControlState.Normal)  
  67.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in_press.png"), forState: UIControlState.Selected)  
  68.         zoomInBtn.setBackgroundImage(UIImage(named: "ic_zoom_in_press.png"), forState: UIControlState.Highlighted)  
  69.         zoomInBtn.tag = 4;  
  70.         zoomInBtn.addTarget(self, action"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)  
  71.         mapView.addSubview(zoomInBtn)  
  72.     }  
  73.       
  74.       
  75.     func btnSelector(sender: UIButton) {  
  76.         switch sender.tag {  
  77.         case 1://定位  
  78.             if centerCoordinate != nil {  
  79.                 mapView.setCenterCoordinate(centerCoordinate, animatedtrue)  
  80.             }  
  81.         case 2://刷新  
  82.             getLocationRoundFlag()  
  83.             mapView.showsUserLocation = true//YES 为打开定位,NO 为关闭定位  
  84.         case 3:  
  85.             if mapView.zoomLevel >= 4 && mapView.zoomLevel <= 19{  
  86.                 mapView.setZoomLevel(mapView.zoomLevel-1, animatedtrue)  
  87.             }else if mapView.zoomLevel >= 3 && mapView.zoomLevel < 4{  
  88.                 mapView.setZoomLevel(3, animatedtrue)  
  89.             }  
  90.         case 4:  
  91.             if mapView.zoomLevel >= 3 && mapView.zoomLevel <= 18{  
  92.                 mapView.setZoomLevel(mapView.zoomLevel+1, animatedtrue)  
  93.             }else if mapView.zoomLevel > 18 && mapView.zoomLevel <= 19{  
  94.                 mapView.setZoomLevel(19, animatedtrue)  
  95.             }  
  96.         default:  
  97.             println("not known ")  
  98.         }  
  99.     }  
  100.       
  101.     func getLocationRoundFlag(){  
  102.     }  
  103.       
  104.     func mapView(mapView: MAMapView!, didUpdateUserLocation userLocation: MAUserLocation!, updatingLocation: Bool) {  
  105.         if updatingLocation {  
  106.             //取出当前位置的坐标  
  107.             println("latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);  
  108.             centerCoordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude,userLocation.coordinate.longitude);  
  109.             mapView.showsUserLocation = false;  
  110.         }  
  111.     }  
  112.       
  113.     //清除数据  
  114.     func clearMapData(){  
  115.         clearMapView()  
  116.         clearSearch()  
  117.     }  
  118.       
  119.     func clearMapView(){  
  120.         mapView.showsUserLocation = false  
  121.         mapView.delegate = nil  
  122.     }  
  123.       
  124.     func clearSearch(){  
  125.         self.search.delegate = nil  
  126.     }  
  127.       
  128.     override func didReceiveMemoryWarning() {  
  129.         super.didReceiveMemoryWarning()  
  130.         // Dispose of any resources that can be recreated.  
  131.     }  
  132. }  

具体实现类

DetailViewController继承BaseMapController

[objc]  view plain  copy
 print ?
  1. //  
  2. //  DetailViewController.swift  
  3. //  SwiftMap  
  4. //  
  5. //  Created by System Administrator on 15/1/22.  
  6. //  Copyright (c) 2015年 jwzhangjie. All rights reserved.  
  7. //  
  8.   
  9. import UIKit  
  10.   
  11. class DetailViewController : BaseMapController {  
  12.       
  13.     var data:NSMutableData!  
  14.       
  15.   
  16.     @IBAction func returnHome(sender: AnyObject) {  
  17.         mapView.removeFromSuperview()  
  18.         self.navigationController?.popViewControllerAnimated(true)  
  19.     }  
  20.       
  21.     @IBAction func segmentChanged(sender: AnyObject) {  
  22.         switch sender.selectedSegmentIndex{  
  23.         case 0://已认证车辆  
  24.             println("0")  
  25.         case 1://全部车辆  
  26.             println("1")  
  27.         default:  
  28.             println("default")  
  29.         }  
  30.     }  
  31.       
  32.     override func viewDidLoad() {  
  33.         super.viewDidLoad()  
  34.         initMapView()  
  35.         initSearchView()  
  36.         initBtns()  
  37.         data = NSMutableData();  
  38.     }  
  39.       
  40.       
  41.     override func getLocationRoundFlag(){  
  42.         var requestUrl:String = "http://api.map.baidu.com/geosearch/v3/nearby?ak=dcZObrBgdDD2s4qLCeC4YVOf&geotable_id=92326&location=121.613461,31.197495&radius=1000000&sortby=distance:1";  
  43. //        var request:NSURLRequest = NSURLRequest(URL:NSURL(string: requestUrl))  
  44. //        var connect:NSURLConnection = NSURLConnection(request: request, delegate: self)!  
  45. //        data = NSMutableData()  
  46.         println(requestUrl)  
  47.           
  48.     }  
  49.   
  50.     override func didReceiveMemoryWarning() {  
  51.         super.didReceiveMemoryWarning()  
  52.         // Dispose of any resources that can be recreated.  
  53.     }  
  54.   
  55.   
  56. }  
记得要在真机上测试

你可能感兴趣的:(swift,高德地图)