在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)限
- 在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>
- 在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涌韩。
如下圖所示:
中間有幾次明明已經(jīng)將BMKLocationKit導(dǎo)入進(jìn)去了,但是卻沒起作用救恨。導(dǎo)致這一步重復(fù)了好幾次贸辈。
再次運行flutter run
命令,項目終于跑起來了肠槽。
添加依賴的這幾個步驟是必不可少的擎淤,但是順序沒有要求。相應(yīng)的錯誤信息都有相應(yīng)的解決辦法秸仙,多嘗試幾次總會解決的嘴拢。
參考文章: flutter 集成百度地圖