现在的位置: 首页 > 综合 > 正文

Swift基础 – - 高德地图实践(一)

2018年09月01日 ⁄ 综合 ⁄ 共 6186字 ⁄ 字号 评论关闭

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

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

需要的库如下截图:


添加头文件

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

#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>

基础地图类实现

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


//
//  BaseMapController.swift
//  SwiftMap
//  地图的基础部分
//  Created by System Administrator on 15/1/24.
//  Copyright (c) 2015年 jwzhangjie. All rights reserved.
//

import UIKit

class 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
        self.view.addSubview(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)
        //定位按钮
        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_press.png"), forState: UIControlState.Highlighted)
        locationBtn.tag = 1;
        locationBtn.addTarget(self, action:"btnSelector:", forControlEvents: UIControlEvents.TouchUpInside)
        mapView.addSubview(locationBtn)
        //刷新按钮
        var refreshBtn:UIButton = UIButton(frame: CGRectMake(15, mapView.frame.size.height-110, 35, 35))
        refreshBtn.setBackgroundImage(UIImage(named: "ic_refresh.png"), forState: UIControlState.Normal)
        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)
        //缩小按钮
        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)
        //放大按钮
        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 sender.tag {
        case 1://定位
            if centerCoordinate != nil {
                mapView.setCenterCoordinate(centerCoordinate, animated: true)
            }
        case 2://刷新
            getLocationRoundFlag()
            mapView.showsUserLocation = true; //YES 为打开定位,NO 为关闭定位
        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:
            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 {
            //取出当前位置的坐标
            println("latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
            centerCoordinate = CLLocationCoordinate2DMake(userLocation.coordinate.latitude,userLocation.coordinate.longitude);
            mapView.showsUserLocation = false;
        }
    }
    
    //清除数据
    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.
    }
}

具体实现类

DetailViewController继承BaseMapController

//
//  DetailViewController.swift
//  SwiftMap
//
//  Created by System Administrator on 15/1/22.
//  Copyright (c) 2015年 jwzhangjie. All rights reserved.
//

import UIKit

class 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://已认证车辆
            println("0")
        case 1://全部车辆
            println("1")
        default:
            println("default")
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initMapView()
        initSearchView()
        initBtns()
        data = NSMutableData();
    }
    
    
    override func getLocationRoundFlag(){
        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";
//        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.
    }


}

记得要在真机上测试



抱歉!评论已关闭.