An integrated Baidu map final effect
Two knowledge points 1 information Retrieval 2 map pin and navigation SDK three integrated Baidu map SDK1 Baidu search Baidu Map SDK
2 registered account (as far as possible) –> if there is no account, it is best to register an account (I do not elaborate on the steps to register the account) 3 because we want to do is and Baidu map related, so directly follow the image below Click Can (here I will introduce the map in iOS related development)
4 Click on the iOS Map SDK inside is some related overview 5 follow the picture click to enter below
6 Related explanations 6.1 Generally we are directly click on the "full download" 6.2 Explanation: "Bitcode version" means: When on the shelves, if the Bitcode version is checked, then on the shelves when packaging, the package sent to contect inside (manage all the applications), It can be uploaded to the App Store, and users can download it. But there is a disadvantage, if the mobile phone application is suitable for the 4 also suitable for the 6p,4 use of the picture is used 2x,6p is the use of 3x pictures. If it's the case, it's all packaged and sent, All of the resources inside are included. But if your phone is 4s, it can cause wasted space. The role of Bitcode is to unpack, that is, according to the corresponding type of phone to the user for the picture, which greatly reduces the size of the application. However, not all SDKs support Bitcode, and it is possible to report bitcode errors when integrating a third-party framework. So what? ? (Change the following to no).
7 Custom Download (if you only want to do one of the functions, just use the custom download)
8 Click All Download 9 get the secret key
10 Create an app (click Submit)
11 Save the acquired key (1yl6aghtbkpupy4fm6pthgo6sfiottzi)
12 Click Development, go back to overview, view Development Guide related information, click Notes
13 When will I use the-OBJC? When the SDK or static libraries are used for categorization, you need to use-OBJC to create links between them (note writing). Follow the path above to complete the project---Edit Active Target in Xcode Build Setting, other Linker flags add-OBJC14 configuration information in Info.plist
15 Add the download good Baidu SDK to the project
16 Follow the steps to import the system's library file
17 Results after import is complete
18 Introducing Mapapi.bundle Resource Files
Mapapi.bundle Location: (in the download of the Baidu SDK Library)
20 have you already imported the resource bundle required for Mapapi.bundl in the 15 step? No, because, if we are 15 steps in the download of the SDK needs to be used for a one-time import, it will not be imported mapapi.bundl, we need one to add.
21 Since we are using swift, if the use of OC requires bridging; Import header file (remember: There is a bug in the header file)
Two sections related to code 100 degree code OC view
//lazy load Private lazy var mapmanager:bmkmapmanager = {r Eturn Bmkmapmanager ()} () Func application (application:uiapplication, Didfinishlaunchingwithoptions launchOp tions: [Nsobject:anyobject]?) Bool {//if you need to focus on network and authorization validation events, set the Generaldelegate parameter let ret = Mapmanager.start (" 1yl6aghtbkpupy4fm6pthgo6sfiottzi ", Generaldelegate:nil) if ret = false { Print ( "manager start failed!" ) } return true }
2 after writing the code, you need to consider what to use to show the map?—-> 1.1 Answer: By looking at Bmkmapview we know it is inherited UIView, then we can drag a uiview into storyboard, Then let the type equals Bmkmapview to show. 3 Error when running: (This notice in the Baidu API is also explained, as long as the configuration under Info.plist can be)
Three information retrieval 1 search behavior: Search the surrounding area what snacks, cinemas, hotels, etc. 2 Information retrieval location
3 Retrieving code-3.1 lazy loading and agent-related definitions
classViewcontroller:uiviewcontroller {//Show Map view @IBOutletWeak Var mapview:bmkmapview!//Lazy loading Private Lazyvar Searcher:bmkpoisearch = {//Initialize the retrieved objectLet searcher = Bmkpoisearch ()//Set up proxySearcher.Delegate= SelfreturnSearcher} ()OverrideFunc viewwillappear (Animated:bool) {Super. Viewwillappear (animated) Mapview.Delegate= self}OverrideFunc viewwilldisappear (Animated:bool) {Super. Viewwillappear (animated)//Set the proxy to nil to avoid memory consumptionMapview.Delegate= nil}}
--3.2 Proxy methods to implement and determine whether the retrieval was successful
///mark:-Implement Proxy method (call back when the user presses long)Extension Viewcontroller:bmkmapviewdelegate {func Mapview (mapview:bmkmapview!, Onlongclick Coordinate:cllocationco ORDINATE2D) {//Initiate searchLetoption= Bmknearbysearchoption ()//Search the first page option. PageIndex =0 number of searches per page option. pagecapacity = - //Retrieved area option. Location = Cllocationcoordinate2dmake (39.915,116.404)//Search for keywords option. Keyword ="movie" //Start SearchLet flag = Searcher.poisearchnearby (option)ifFlag {print ("Peripheral Search sent successfully") }Else{Print ("Peripheral Retrieval send failed") } }}
-3.3 Results Callback
extension viewcontroller:bmkpoisearchdelegate {func ongetpoiresult (searcher:bmkpoisearch!, result poiresult:bmkpoiresult!, ErrorCode: Bmksearcherrorcode) {if ErrorCode = bmk_search_no_error {//handling normal results print ( "Fetch to data" )}else if errorCode = = bmk_search_ambiguous _keyword {print ( "starting point ambiguous" )}else {print ( "Sorry, no results found" /span>)}}}
4 Note One: If you follow the Baidu provided above the code to write the location, there is a problem. Because when the view is not finished loading, it is requested to retrieve, this will not be successful. (Modify writing position) 5 Note two: Need to be in the view will disappear, the proxy assignment is empty, otherwise it will affect the memory four navigation SDK1 Click on the image below to access the Navigation SDK interface
2 Get the secret key (the part that has already been acquired, which is not needed again) 3 where to download the Navigation SDK
4 Import the downloaded third-party navigation SDK files into the app
5 Importing related frameworks
6 SDK Import Project, the compilation will still error, this is we only need to set the following image is OK, now basically is the arc mode. If it is in non-arc mode, it needs to be configured in build settings, "Other Linker Flags" add "- OBJC "-6.1 Arc mode configuration
-6.2 Non-ARC mode configuration
7 configuration in the Info.plist file (front and rear settings) 8 #import "BNCoreServices.h" into the bridge file in the five map pins and navigation code (this part of the story is not good, I directly to everyone to write the overall code)
Call the method func Mapview (mapview:bmkmapview!, annotationviewforbubble view:bmkannotationview!) When you click the Bullet box {//Get the coordinates of the current pin let coodinate = view. Annotation. CoordinateNode array var nodesarray = [Bnrouteplannode] ()//start Let Startnode = Bnrouteplannode () startnode. POS= Bnposition () startnode. POS. x= Coodinate. LongitudeStartnode. POS. Y= Coodinate. Latitude+0.1Startnode. POS. EType= Bncoordinate_baidumapsdk Nodesarray. Append(startnode)//end point Let Endnode = Bnrouteplannode () endnode. POS= Bnposition () endnode. POS. x= Coodinate. LongitudeEndnode. POS. Y= Coodinate. LatitudeEndnode. POS. EType= Bncoordinate_baidumapsdk Nodesarray. Append(Endnode)//Initiate path planning bncoreservices. Routeplanservice(). Startnavirouteplan(Bnrouteplanmode_recommend, Navinodes:nodesarray, Time:nil, Delegete:self, Userinfo:nil)} Zone changes call the method func Mapview (mapview:bmkmapview!, regiondidchangeanimated animated:bool) {print (Mapview. Region. Span)}}extension Viewcontroller:bnnavirouteplandelegate {func routeplandidfinished (userInfo: [nsobject:anyobject]! ) {Bncoreservices. Uiservice(). SHOWNAVIUI(Bn_navitypesimulator, Delegete:nil, Isneedlandscape:true)}} Extension Viewcontroller:bmkpoisearchdelegate {func ongetpoiresult (searcher:bmkpoisearch!, result Poiresult:bmkpoi result!, Errorcode:bmksearcherrorcode) {if ErrorCode = = bmk_search_no_error {//Handle normal result let Poiinfos = Poiresult. Poiinfolistas! [Bmkpoiinfo]//traverse for PoiinfoinchPoiinfos {//Add a pointannotation let annotation = bmkpointannotation () Annota tion. Coordinate= Poiinfo. PTAnnotation. Title= Poiinfo. NameAnnotation. Subtitle= Poiinfo. AddressAdd a PIN Mapview. Addannotation(annotation)} }else if ErrorCode = = Bmk_search_ambiguous_keyword {print ("The starting point is ambiguous")}else {print ("Sorry, no results found") } }}
1 after writing the code to run, error messages, the following is a false message displayed.
-1.1 FIX: Open background mode without turning on background mode
2 left two issues--2.1 bullet box prompt TTS failed?—-> 2.2 No sound during navigation: Click on The Voice broadcast in the development document, create an app to fill in the Bundleid and Key3 solve the problem One:(voice broadcast)-3.1:
-3.2:
-3.3:
4 Solving the problem two: (Frame failure)
Six Summary 1 This part can be said to be a complete integration of the third-party library implementation of the map navigation, there are some problems solved is not very good, I hope to see my blog can give me some suggestions, or we can discuss together. 2 Finally, if you think I wrote the blog is OK, trouble everyone more attention to my official blog, Thank you!!!!
Integrated Baidu Map SDK (Swift)