1. 程式人生 > >用Swift語言使用IOS百度地圖(折線的使用方法很特別)

用Swift語言使用IOS百度地圖(折線的使用方法很特別)

摘要

本文旨在講解如何使用Swift語言單獨整合百度地圖的SDK

前言

百度地圖iOS SDK是一套基於armv7、armv7s、arm64(自v2.5.0版本)處理器裝置的應用程式介面,不僅提供構建地圖的基本介面,還提供POI搜尋、地理編碼、路線規劃、定位、本地覆蓋物繪製等服務,自v2.0.0開始為向量渲染的3D地圖,並新增了向量離線地圖下載功能介面。

您可以使用百度地圖iOS SDK開發適用於移動裝置的地圖應用,通過介面,您可以輕鬆訪問百度服務和資料,構建功能豐富、互動性強的地圖應用程式。百度地圖iOS SDK提供的功能如下:

地圖:提供地圖展示和地圖操作功能;

POI檢索:支援周邊檢索、區域檢索和城市內興趣點檢索;

地理編碼:提供經緯度和地址資訊相互轉化的功能介面;

線路規劃:支援公交、駕車、步行三種方式的線路規劃;

覆蓋物圖層:支援在地圖上新增覆蓋物(標註、幾何圖形、熱力圖、地形圖圖層等),展示更豐富的LBS資訊;

定位:獲取當前位置資訊,並在地圖上展示(支援普通、跟隨、羅盤三種模式);

離線地圖:使用離線地圖可節省使用者流量,提供更好的地圖展示效果;

導航:支援調啟百度地圖客戶端導航和調啟Web頁面導航(H5導航);

LBS雲檢索:支援查詢儲存在LBS雲內的自有資料;

特色功能:提供短串分享、Place詳情檢索、熱力圖等特色功能,幫助開發者搭建功能更加強大的應用;

百度地圖是我使用LBS應用中開放度最高的地圖之一了,但是這樣的一個便捷好用SDK卻沒有一個完整好用的Swift的Demo教程,確實很讓人傷心,百度沒有跟隨蘋果的節奏,給我們開發者帶來了一些不便,我的這篇文章旨在方便同道中人共同探討學習。

編輯器版本

XCode Version 6.2 (6C131e)

百度地圖版本

BaiduMap_IOSSDK_v2.6.0_All

下載地址

http://developer.baidu.com/map/index.php?title=iossdk/sdkiosdev-download

duang的一聲地圖演示開始

一、申請密匙

至於密匙如何申請請大家參考百度的密匙申請文件,這點我就跳過不提了

密匙申請地址

http://lbsyun.baidu.com/apiconsole/key

二、地圖使用

新建一個專案叫SimpleMovie


因為我們使用的Swift,而百度地圖使用的全部是以前的ObjectC開發的,所以我們需要引入一個ObjectC寫的檔案,並把其中的一個實現檔案(字尾名為m的)的字尾名改為mm的


在SimpleMovie-Bridging-Header.h檔案中寫入以下檔案

#import "BMapKit.h"
參考百度的庫檔案引入,倒入相關包



匯入成功後,我們就開始我們的HelloBaiduMap

在將要顯示的地圖的介面的viewDidLoad方法中中初始化地圖管理器,並設定地圖

百度的寫法

在您的AppDelegate.h檔案中新增BMKMapManager的定義

@interface BaiduMapApiDemoAppDelegate : NSObject <UIApplicationDelegate> {   
       UIWindow *window;    
       UINavigationController *navigationController;     
       BMKMapManager* _mapManager;    
}

在您的AppDelegate.m檔案中新增對BMKMapManager的初始化,並填入您申請的授權Key,示例如下

- (BOOL)application:(UIApplication *)application   
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {       // 要使用百度地圖,請先啟動BaiduMapManager  
    _mapManager = [[BMKMapManager alloc]init];   
// 如果要關注網路及授權驗證事件,請設定     generalDelegate引數  
    BOOL ret = [_mapManager start:@"在此處輸入您的授權Key"  generalDelegate:nil];  
    if (!ret) {  
        NSLog(@"manager start failed!");  
    }  
// Add the navigation controller's view to the window and display.  
    [self.window addSubview:navigationController.view];  
    [self.window makeKeyAndVisible];  
   return YES;  
}

我們的Swift寫法

overridefunc viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

self.navigationItem.title = "線上影院"

//        初始化地圖管理器

self.mapManager = BMKMapManager()

//        地圖管理器是否初始化成功

var ret = mapManager.start("填入你申請的key", generalDelegate: nil)

//        初始化地圖

self.mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))

self.view = mapView;

    }

好了我們的地圖就可以顯示了,當然你也可以設定一些引數,比如說時地圖中心,縮放等級。。。



如果想要開啟擠出適量圖和衛星圖就可以這樣設定,當然百度地圖預設的是基礎向量圖

//        BMKMapTypeStandard   = 1,         // < 標準地圖

//        BMKMapTypeSatellite  = 2,         // < 衛星地圖

//        self.mapView.mapType = 2          //在這裡無法使用這兩種型別,只能使用數字代替


開啟實時交通圖

//      self.mapView.trafficEnabled = true  //開啟實時交通圖

//        self.mapView.trafficEnabled = false //關閉實時交通圖

開啟城市熱力圖

 self.mapView.baiduHeatMapEnabled = true //開啟城市熱力圖

//        self.mapView.baiduHeatMapEnabled = false //關閉城市熱力圖


好了我們需要關注的東西來了“地圖標註”

1、首先我們顯示地圖的這個類需要實現BMKMapViewDelegate方法,以實現和地圖的互動


2、新增顯示的標註資訊


3、新增顯示標註的塗層


最終顯示結果


好了接下來我們來實現一個折線圖形,這個方法和我們平時用的時候是不一樣的

這個程式碼片段是百度提供的

- (void)viewDidLoad {     
    [super viewDidLoad];    
    // 新增折線覆蓋物    
    CLLocationCoordinate2D coors[2] = {0}; 
    coors[0].latitude = 39.315; 
    coors[0].longitude = 116.304;
    coors[1].latitude = 39.515; 
    coors[1].longitude = 116.504;
    BMKPolyline* polyline = [BMKPolyline polylineWithCoordinates:coors count:2];
    [_mapView addOverlay:polyline];    
}    
// Override    
- (BMKOverlayView *)mapView:(BMKMapView *)mapView viewForOverlay:(id <BMKOverlay>)overlay{    
   if ([overlay isKindOfClass:[BMKPolyline class]]){    
        BMKPolylineView* polylineView = [[[BMKPolylineView alloc] initWithOverlay:overlay] autorelease];    
        polylineView.strokeColor = [[UIColor purpleColor] colorWithAlphaComponent:1];
        polylineView.lineWidth = 5.0;
 
        return polylineView;    
   }    
   return nil;    
}
這個是我們自己實現的

overridefunc viewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

self.navigationItem.title = "線上影院"

//        初始化地圖管理器

self.mapManager = BMKMapManager()

//        地圖管理器是否初始化成功

var ret = mapManager.start("填入你申請的key", generalDelegate: nil)

//        初始化地圖

self.mapView = BMKMapView(frame: CGRect(x: 0, y: 0, width: 320, height: 480))

self.view = mapView;

// 新增折線覆蓋物

var coor1_latitude:CLLocationDegrees = 39.915

var coor1_longitude:CLLocationDegrees = 116.404

var coor1:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: coor1_latitude, longitude: coor1_longitude)

var coor2_latitude:CLLocationDegrees = 39.515

var coor2_longitude:CLLocationDegrees = 116.504

var coor2:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: coor2_latitude, longitude: coor2_longitude)

var c:[CLLocationCoordinate2D] = []

        c.append(coor1)

        c.append(coor2)

//這裡的引數Coordinate用法是用的C語言的指標方式,具體請參考蘋果官網給出的解釋

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html

var polyLine:BMKPolyline = BMKPolyline(coordinates: &c, count: 2)

self.mapView.addOverlay(polyLine)

    }


func mapView(mapView: BMKMapView!, viewForOverlay overlay: BMKOverlay!) -> BMKOverlayView! {

if overlay.isKindOfClass(BMKPolyline){

var polyLineView:BMKPolylineView = BMKPolylineView(overlay: overlay)

            polyLineView.strokeColor = UIColor.purpleColor()

            polyLineView.lineWidth = 5.0

return polyLineView

        }

returnnil

    }


效果顯示如圖