Flutter 百度定位示例代码与工具封装

该文章介绍了如何在Flutter项目中配置百度地图API以实现定位服务。配置包括在`pubspec.yaml`添加依赖,设置`AndroidManifest.xml`中的权限和AK,以及使用示例代码展示如何获取单次和连续定位。同时,文章指出在虚拟机中可能遇到经纬度负数的问题,并提及H5Web上不支持此功能,需要额外处理兼容性问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境说明:

  1. 纯Flutter项目
  2. minSdkVersion:20 (可以在android/app/build.gradle中配置)
  3. sdk: ‘>=2.19.6 < 3.0.0’ (pubspec.yaml)
  4. 真机调试测试【开启USB调试 + 开发者模式】(我在虚拟机中测试数据有很大的问题,经纬度会有负数—_—)

依赖配置

# pubspec.yaml 依赖
dependencies:
  flutter:
    sdk: flutter
  get: ^4.6.5
  logger: ^1.3.0
  permission_handler: ^10.2.0
  flutter_bmflocation: ^3.2.1   #定位
  flutter_baidu_mapapi_map: ^3.2.1   #基础地图依赖
  flutter_baidu_mapapi_utils: ^3.2.0   #计算地图依赖
<!-- 
	android\app\src\main\AndroidManifest.xml
	主要配置AK和权限
 -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kzou.track_map">
   <application
        android:label="track_map"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <!-- 获取AK的文档: https://lbsyun.baidu.com/index.php?title=ios-locsdk/guide/create-project/key -->
        <meta-data
            android:name="flutterEmbedding"
            android:value
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值