flutter百度地圖集成

在flutter項目中用到了百度地圖带膜,但是在集成過程中遇到了問題,所以將解決問題的步驟記錄下來。一是為了以后別的項目中用到的話有個參考,還有一個就是給遇到相同問題的小伙伴提供一點點幫助姚淆。

百度地圖集成官網(wǎng)地址:點擊跳轉(zhuǎn)

1.百度地圖主要是定位SDK。

2.以下步驟都是以新建項目在iPhone手機上運行為基準(zhǔn)牙咏。

1早直、新建項目

首先是新建項目,通過Android Studio創(chuàng)建flutter Application的時候沉填,默認(rèn)是沒有選擇Swift和Kotlin的疗隶,也就是iOS和Android默認(rèn)使用的語言是Objective-C和Java。

注意:如果第三方插件在iOS中使用的是Swift語言翼闹,跟項目不一致斑鼻,則需要將項目通過Xcode打開,然后新建一個swift文件猎荠,名字隨意坚弱,Xcode會自動提示是否創(chuàng)建橋接文件。

使用VS Code創(chuàng)建flutter項目的話关摇,在我的電腦上是默認(rèn)使用的Swift和Kotlin語言的荒叶,如果創(chuàng)建完項目想修改未Objective-C和Java的話,需要將項目目錄項的ios和android兩個文件夾刪除输虱,然后在終端中使用以下命令:flutter create -i objc -a java .即可些楣。

2、添加依賴

根據(jù)官網(wǎng)的步驟,在pubspec.yaml文件中加入以下依賴:

flutter_bmflocation: ^1.0.2

執(zhí)行命令flutter package get去加載依賴愁茁。
然后執(zhí)行命令flutter run來運行項目蚕钦。意外的報錯了。
報錯信息如下:

CocoaPods' output:
?
      Preparing

    Analyzing dependencies

    Inspecting targets to integrate
      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)

    Finding Podfile changes
      A flutter_bmfbase
      A flutter_bmflocation
      R bdmap_location_flutter_plugin
      - Flutter
      - permission_handler

    Fetching external sources
    -> Fetching podspec for `Flutter` from `Flutter`
    -> Fetching podspec for `flutter_bmfbase` from `.symlinks/plugins/flutter_bmfbase/ios`
    -> Fetching podspec for `flutter_bmflocation` from `.symlinks/plugins/flutter_bmflocation/ios`
    [!] No podspec found for `flutter_bmflocation` in `.symlinks/plugins/flutter_bmflocation/ios`

看到最后一行鹅很,在ios目錄下打開.symlinks/plugins/flutter_bmflocation/ios路徑嘶居,發(fā)現(xiàn)在該路徑下面并沒有flutter_bmflocation.podspec,反而是有一個bdmap_location_flutter_plugin.podspec促煮,第一直覺就是這里有問題邮屁。
為了驗證這個問題,就新建了一個文件污茵,命名為:flutter_bmflocation.podspec樱报,然后將bdmap_location_flutter_plugin.podspec中的內(nèi)容拷貝一份到flutter_bmflocation.podspec,并將文件內(nèi)的s.name修改未flutter_bmflocation泞当。再次運行flutter run命令迹蛤,發(fā)現(xiàn)還是報錯了。????
不過這次報錯的信息跟上次的不同了襟士,內(nèi)容如下:

CocoaPods' output:
?
      Preparing

    Analyzing dependencies

    Inspecting targets to integrate
      Using `ARCHS` setting to build architectures of target `Pods-Runner`: (``)

    Fetching external sources
    -> Fetching podspec for `Flutter` from `Flutter`
    -> Fetching podspec for `flutter_bmfbase` from `.symlinks/plugins/flutter_bmfbase/ios`
    -> Fetching podspec for `flutter_bmflocation` from `.symlinks/plugins/flutter_bmflocation/ios`
    -> Fetching podspec for `permission_handler` from `.symlinks/plugins/permission_handler/ios`

    Resolving dependencies of `Podfile`
      CDN: trunk Relative path: CocoaPods-version.yml exists! Returning local because checking is
      only perfomed in repo update
      CDN: trunk Relative path: all_pods_versions_3_7_9.txt exists! Returning local because
      checking is only perfomed in repo update
      CDN: trunk Relative path: Specs/3/7/9/BaiduMapKit/5.4.0/BaiduMapKit.podspec.json exists!
      Returning local because checking is only perfomed in repo update
      CDN: trunk Relative path: Specs/3/7/9/BaiduMapKit/5.4.0/BaiduMapKit.podspec.json exists!
      Returning local because checking is only perfomed in repo update

    Comparing resolved specification to the sandbox manifest
      A BaiduMapKit
      A Flutter
      A flutter_bmfbase
      A flutter_bmflocation
      A permission_handler

    Downloading dependencies

    -> Installing BaiduMapKit (5.4.0)
      > Copying BaiduMapKit from
      `/Users/lixiaokang/Library/Caches/CocoaPods/Pods/Release/BaiduMapKit/5.4.0-40a43` to
      `Pods/BaiduMapKit`

    -> Installing Flutter (1.0.0)

    -> Installing flutter_bmfbase (0.0.1)

    -> Installing flutter_bmflocation (0.0.1)

    -> Installing permission_handler (4.4.0+hotfix.4)
      - Running pre install hooks
    [!] The 'Pods-Runner' target has transitive dependencies that include statically linked
    binaries:
    (/Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/thirdlibs/libcrypto.a,
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/thirdlibs/libssl.a,
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/BaiduMapAPI_Base.framework,
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/BaiduMapAPI_Cloud.framework
    ,
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/BaiduMapAPI_Map.framework,
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/BaiduMapAPI_Search.framewor
    k, and
    /Users/lixiaokang/Desktop/mapdemo1/ios/Pods/BaiduMapKit/BaiduMapKit/BaiduMapAPI_Utils.framework
    )
    ...

看到下面盗飒,猜測是百度地圖的某些庫沒有添加進(jìn)去。
畫面轉(zhuǎn)到百度地圖開放平臺陋桂,看到iOS端需要手動部署百度iOS定位SDK逆趣,具體步驟可參照iOS定位SDK手動部署說明
但是不一樣的是嗜历,BMKLocationKit.framework下載下來之后要放到Pods目錄里面宣渗,我是跟其他的framework放到一起了,都在BaiduMapKit目錄下梨州。還有一個不同之后是添加BMKLocationKit的不是工程名下的TARGET痕囱,而是Pods下的TARGETS,也就是flutter_bmflocation暴匠。

注意:這時候有可能因為pod install失敗而沒有Pods鞍恢,可以先進(jìn)行下面的步驟∶拷眩或者先引入別的庫進(jìn)行Pod install之后再刪掉帮掉。

然后進(jìn)行下面的步驟,需要引入一些系統(tǒng)庫文件窒典。
打開Xcode蟆炊,選擇項目名稱,也就是Runner瀑志,選擇TARGETS->Build Phases->Link Binary With Libraries盅称,點擊+肩祥,添加以下系統(tǒng)庫文件:
CoreLocation.framework
SystemConfiguration.framework
Security.framework
libsqlite3.0.tbd(xcode7以前為 libsqlite3.0.dylib)
CoreTelephony.framework
libc++.tbd(因需適配iOS12,需要將libstdc++.6.0.9.tbd 更新為libc++.tbd)
AdSupport.framework

申請權(quán)限

  1. 在Info.plist中添加定位權(quán)限缩膝,這個根據(jù)需要去選擇相關(guān)的方式:
  • NSLocationWhenInUseUsageDescription:表示應(yīng)用在前臺的時候可以搜到更新的位置信息;
    • NSLocationAlwaysUsageDescription:表示應(yīng)用在前臺和后臺(suspend 或 terminated)都可以獲取到更新的位置數(shù)據(jù)岸霹;
    • NSLocationAlwaysAndWhenInUseUsageDescription:申請永久定位權(quán)限疾层,以便應(yīng)用在前臺和后臺都可以獲取位置數(shù)據(jù);
      代碼如下(文字說明根據(jù)項目隨意更改):
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>請求定位權(quán)限贡避,獲取當(dāng)前位置</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>請求定位權(quán)限痛黎,獲取當(dāng)前位置</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>請求定位權(quán)限,獲取當(dāng)前位置</string>
  1. 在TARGETS->Build Settings->Other Linker Flags 中添加-ObjC刮吧。

再次運行flutter run命令湖饱,報錯信息如下:

Xcode's output:
?
    /Users/lixiaokang/flutter/.pub-cache/hosted/pub.flutter-io.cn/permission_handler-4.4.0+hotfix.4
    /ios/Classes/strategies/NotificationPermissionStrategy.m:71:5: warning:
    'UIRemoteNotificationType' is deprecated: first deprecated in iOS 8.0 - Use UserNotifications
    Framework's UNAuthorizationOptions for user notifications and registerForRemoteNotifications
    for receiving remote notifications instead. [-Wdeprecated-declarations]
        UIRemoteNotificationType type = [[UIApplication sharedApplication]
        enabledRemoteNotificationTypes];
        ^
    In module 'UIKit' imported from /Users/lixiaokang/Desktop/mapdemo2/ios/Pods/Target Support
    Files/permission_handler/permission_handler-prefix.pch:2:
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS1
    3.4.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:69:32: note:
    'UIRemoteNotificationType' has been explicitly marked deprecated here
    typedef NS_OPTIONS(NSUInteger, UIRemoteNotificationType) {
                                   ^
    /Users/lixiaokang/flutter/.pub-cache/hosted/pub.flutter-io.cn/permission_handler-4.4.0+hotfix.4
    /ios/Classes/strategies/NotificationPermissionStrategy.m:71:72: warning:
    'enabledRemoteNotificationTypes' is deprecated: first deprecated in iOS 8.0 - Use
    -[UIApplication isRegisteredForRemoteNotifications] and UserNotifications Framework's
    -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] to retrieve
    user-enabled remote notification and user notification settings [-Wdeprecated-declarations]
        UIRemoteNotificationType type = [[UIApplication sharedApplication]
        enabledRemoteNotificationTypes];
                                                                           ^
    In module 'UIKit' imported from /Users/lixiaokang/Desktop/mapdemo2/ios/Pods/Target Support
    Files/permission_handler/permission_handler-prefix.pch:2:
    /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS1
    3.4.sdk/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h:229:1: note:
    'enabledRemoteNotificationTypes' has been explicitly marked deprecated here
    - (UIRemoteNotificationType)enabledRemoteNotificationTypes API_DEPRECATED("Use -[UIApplication
    isRegisteredForRemoteNotifications] and UserNotifications Framework's
    -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] to retrieve
    user-enabled remote notification and user notification settings", ios(3.0, 8.0))
    API_UNAVAILABLE(tvos);
    ^
    2 warnings generated.
    /Users/lixiaokang/flutter/.pub-cache/hosted/pub.flutter-io.cn/flutter_bmflocation-1.0.2/ios/Cla
    sses/BdmapLocationFlutterPlugin.m:3:9: fatal error: 'BMKLocationkit/BMKLocationComponent.h'
    file not found
    #import "BMKLocationkit/BMKLocationComponent.h"
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    1 error generated.
    note: Using new build system
    note: Building targets in parallel
    note: Planning build
    note: Constructing build description
    warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 7.0, but the range of
    supported deployment target versions is 8.0 to 13.4.99. (in target 'BaiduMapKit' from project
    'Pods')

我這里是在前面沒有添加BMKLocationKit.framework,所以報這個錯杀捻。
看到錯誤信息是找不到BMKLocationComponent.h【幔現(xiàn)在就用到前面下載的BMKLocationKit.framework了。
先找到項目的路徑致讥,打開ios->Pods目錄仅仆,看到有個文件夾:BaiduMapKit。這個里面放著百度地圖的一些SDK和framework垢袱,將下載好的BMKLocationKit.framework拖進(jìn)去墓拜。然后打開Xcode,選中Pods请契,在TARGETS中選擇flutter_bmflocation咳榜,選擇Build Phases -> Link Binary With Libraries,點擊+號爽锥,選擇Add Other -> Add Files涌韩。
如下圖所示:

截屏2020-08-21 18.15.26.png

中間有幾次明明已經(jīng)將BMKLocationKit導(dǎo)入進(jìn)去了,但是卻沒起作用救恨。導(dǎo)致這一步重復(fù)了好幾次贸辈。

再次運行flutter run命令,項目終于跑起來了肠槽。

添加依賴的這幾個步驟是必不可少的擎淤,但是順序沒有要求。相應(yīng)的錯誤信息都有相應(yīng)的解決辦法秸仙,多嘗試幾次總會解決的嘴拢。

參考文章: flutter 集成百度地圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市寂纪,隨后出現(xiàn)的幾起案子席吴,更是在濱河造成了極大的恐慌赌结,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,968評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件孝冒,死亡現(xiàn)場離奇詭異柬姚,居然都是意外死亡,警方通過查閱死者的電腦和手機庄涡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,601評論 2 382
  • 文/潘曉璐 我一進(jìn)店門量承,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人穴店,你說我怎么就攤上這事撕捍。” “怎么了泣洞?”我有些...
    開封第一講書人閱讀 153,220評論 0 344
  • 文/不壞的土叔 我叫張陵忧风,是天一觀的道長。 經(jīng)常有香客問我球凰,道長狮腿,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,416評論 1 279
  • 正文 為了忘掉前任弟蚀,我火速辦了婚禮蚤霞,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘义钉。我一直安慰自己昧绣,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,425評論 5 374
  • 文/花漫 我一把揭開白布捶闸。 她就那樣靜靜地躺著夜畴,像睡著了一般。 火紅的嫁衣襯著肌膚如雪删壮。 梳的紋絲不亂的頭發(fā)上贪绘,一...
    開封第一講書人閱讀 49,144評論 1 285
  • 那天,我揣著相機與錄音央碟,去河邊找鬼税灌。 笑死,一個胖子當(dāng)著我的面吹牛亿虽,可吹牛的內(nèi)容都是我干的菱涤。 我是一名探鬼主播,決...
    沈念sama閱讀 38,432評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼洛勉,長吁一口氣:“原來是場噩夢啊……” “哼粘秆!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起收毫,我...
    開封第一講書人閱讀 37,088評論 0 261
  • 序言:老撾萬榮一對情侶失蹤攻走,失蹤者是張志新(化名)和其女友劉穎殷勘,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昔搂,經(jīng)...
    沈念sama閱讀 43,586評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡玲销,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,028評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了巩趁。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片痒玩。...
    茶點故事閱讀 38,137評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖议慰,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奴曙,我是刑警寧澤别凹,帶...
    沈念sama閱讀 33,783評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站洽糟,受9級特大地震影響炉菲,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜坤溃,卻給世界環(huán)境...
    茶點故事閱讀 39,343評論 3 307
  • 文/蒙蒙 一拍霜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧薪介,春花似錦祠饺、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,333評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至记劈,卻和暖如春勺鸦,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背目木。 一陣腳步聲響...
    開封第一講書人閱讀 31,559評論 1 262
  • 我被黑心中介騙來泰國打工换途, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人刽射。 一個月前我還...
    沈念sama閱讀 45,595評論 2 355
  • 正文 我出身青樓军拟,卻偏偏與公主長得像,于是被迫代替她去往敵國和親柄冲。 傳聞我的和親對象是個殘疾皇子吻谋,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,901評論 2 345