Swift-the configuration and use of the Baidu Map SDK

Source: Internet
Author: User
Tags uikit

First, the application API Key

First we have to go to the Baidu Map open platform to apply for an API Key, for our program use. Address: http://lbsyun.baidu.com/


Second, the project configuration

1, modify Info.plist
Because the iOS9 is switched to more secure HTTPS, the map SDK can be used normally in iOS9. You need to add the following configuration in "Info.plist":

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

2, download iOS SDK

Baidu Map SDK is the framework form, address: Click here to download.
Original: Swift-Baidu Map SDK Configuration and use (with sample)

3, import as needed. Framework Package

Where Baidumapapi_base.framework is the base package, it must be imported. Other subcontracting can be imported on demand.
Here we only import baidumapapi_base.framework, baidumapapi_map.framework.
Original: Swift-Baidu Map SDK Configuration and use (with sample)

4, add dependent libraries

Because the Baidu Map SDK provides positioning function and animation effect, using OpenGL rendering. So we're going to add the following system libraries in Project-> build phases-> Link Binary with libraries:
Corelocation.framework, Quartzcore.framework, Opengles.framework, Systemconfiguration.framework, Coregraphics.framework, Security.framework, Libsqlite3.0.tbd, Coretelephony.framework, LIBSTDC++.6.0.9.TBD
Original: Swift-Baidu Map SDK Configuration and use (with sample)

5, the introduction of Mapapi.bundle resource files
Mapapi.bundle stores the location, default pin callout View and route key point of the resource picture, also stores the vector map to draw the necessary resource files.
If we do not need to use the built-in picture display feature, you can delete the image folder in the bundle file.
You can also arbitrarily replace or delete the picture file of the image folder in the bundle according to the specific requirements.
(1) Select the project name, select Add Files to "project name" in the right key menu ...


(2) Select the Baidumapapi_map.framework-> resources-> mapapi.bundle file and tick the Copy items if needed check box, click the Add button, You can add a resource file to your project.
Original: Swift-Baidu Map SDK Configuration and use (with sample)

6, new bridge Connector file

We need to import the required library files in this header file, the details are as follows. (about the configuration of the bridge header file here's not much to say)

#import <BaiduMapAPI_Base/BMKBaseComponent.h>//introduction of Base-related all header files
#import <BaiduMapAPI_Map/BMKMapComponent.h>//Map features all header files
#import <BaiduMapAPI_Map/BMKMapView.h>//Only the required single head files are introduced
/****** The following few temporarily do not need ******/
#import <BaiduMapAPI_Search/BMKSearchComponent.h>//introduction of Retrieval function All header files
#import <BaiduMapAPI_Cloud/BMKCloudSearchComponent.h>//introduction of cloud Retrieval function All header files
#import <BaiduMapAPI_Location/BMKLocationComponent.h>//introduction of positioning function All header files
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>//Introduction of computing Tools All header files
#import <BaiduMapAPI_Radar/BMKRadarComponent.h>//introduction of peripheral radar function All header files

Third, the sample effect chart

1, when the program starts automatically displays a full screen of Baidu map. Map Center Point coordinates set for the Shanghai Oriental Pearl Tower.
2, also added a callout (custom coordinates) on the map.



Four, sample code
1,appdelegate.swift
Use the authorization Key we previously requested to declare and initialize the Bmkmapmanager.


Import Uikit

@UIApplicationMain
Class Appdelegate:uiresponder, Uiapplicationdelegate, bmkgeneraldelegate {

var Window:uiwindow?

var _mapmanager:bmkmapmanager?

Func Application (Application:uiapplication,
Didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]? -> Bool {

_mapmanager = Bmkmapmanager ()
If you want to focus on network and authorization validation events, set Generaldelegate parameters
Let ret = _mapmanager? Start ("Mrytrhzazult5aojf5tmsxxxxxxxxxxx",
Generaldelegate:self)
if ret = = False {
NSLog ("Manager start failed!")
}
return True
}
//...............

2,viewcontroller.swift

Here we create add Baidu map View and set display area as well as marker points.


Import Uikit

Class Viewcontroller:uiviewcontroller {

Baidu Map View
var _mapview:bmkmapview?

Override Func Viewdidload () {
Super.viewdidload ()
Add Baidu Map, and full screen display
_mapview = Bmkmapview (Frame:cgrect (x:0, y:0,
Width:self.view.frame.width,
Height:self.view.frame.height))
Self.view.addSubview (_mapview!)
}

Override func Viewdidappear (Animated:bool) {
Map Center Point coordinates
Let center = cllocationcoordinate2d (latitude:31.245087, longitude:121.506656)
Set the display range of the map (smaller and more accurate)
Let span = Bmkcoordinatespan (latitudedelta:0.05, longitudedelta:0.05)
Set up map final display area
Let region = Bmkcoordinateregion (Center:center, Span:span)
_mapview? Region = Region

Add a marker point (pointannotation)
let annotation = Bmkpointannotation ()
var coor = cllocationcoordinate2d ()
Coor.latitude = 31.254087
Coor.longitude = 121.512656
Annotation.coordinate = coor
Annotation.title = "There is a wild pikachu here"
_mapview!. Addannotation (annotation)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.