flutter 集成高德获取定位

安装依赖:

flutter pub add amap_flutter_location

utils:location.dart

import 'package:permission_handler/permission_handler.dart';
import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';

class LocationUtils {
    static final AMapFlutterLocation flutterLocation = AMapFlutterLocation();
    static PermissionStatus? permissionStatus;
    static late StreamSubscription<Map<String, Object>> locationListener;

    
    static getLocation (String androidKey, String iosKey, Function(dynamic result) onLocationChanged){
        _setPrivacy(androidKey, iosKey);
        _requestPermission(onLocationChanged);
    }

    static void stopLocation (){
        _stopLocation();
    }


    //  设置高德apiKey
    static void _setPrivacy (String androidKey, String iosKey){
        AMapFlutterLocation.setApiKey(
            androidKey,
            iosKey
        );
        AMapFlutterLocation.updatePrivacyAgree(true);
        AMapFlutterLocation.updatePrivacyShow(true, true);
    }

    //  获取系统权限
    static _requestPermission (Function(dynamic result) onLocationChanged) async {
        permissionStatus = await Permission.location.status;
        if(permissionStatus == PermissionStatus.granted) {
            _requestLocation(onLocationChanged);
        } else {
            permissionStatus = await Permission.location.request();
            switch (permissionStatus) {
                case PermissionStatus.denied:
                     print('请开启位置权限');
                     break;
                case PermissionStatus.granted:
                     _requestLocation(onLocationChanged);
                     break;
                case PermissionStatus.limited:
                     print('请开启位置权限');
                     break;
                default:
                     _requestLocation(onLocationChanged);
                     break;
            }     

        }
    }

    //  设置配置项
    static void _setLocationOption () {
        AMapLocationOption locationOption = AMapLocationOption();
        flutterLocation.setLocationOption(locationOption);
    }

    //  获取实时位置
    static _requestLocation (Function(dynamic result) onLocationChanged) {
        _setLocationOption();
        flutterLocation.startLocation();
        locationListener = flutterLocation.onLocationChanged().listen((result){
            onLocationChanged(result);
        });
    
    }

    //  停止定位
    static void _stopLocation (){
        flutterLocation.stopLocation();
        if(locationListener != null)
            locationListener?.cancel();
    }

}

使用:

import 'package:sucoty_app/utils/location.dart';

//  业务逻辑
LocationUtils.getLocation("androidKey", "iosKey", (result){
    //  LocationUtils.stopLocation();
    ...  //  TODO OTHER THINGS    
});

Android端
在flutter工程的/android/app/src/main/AndroidManifest.xml里声明如下权限:

<!--允许访问网络,必选权限-->
<uses-permission android:name="android.permission.INTERNET" />  

<!--允许获取精确位置,精准定位必选-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
 
<!--允许获取粗略位置,粗略定位必选-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<!--允许获取设备和运营商信息,用于问题排查和网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />    

<!--允许获取网络状态,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<!--允许获取wifi网络信息,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

<!--允许获取wifi状态改变,用于网络定位(无gps情况下的定位),若需网络定位功能则必选-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> 

<!--后台获取位置信息,若需后台定位则必选-->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> 

<!--用于申请调用A-GPS模块,卫星定位加速-->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> 

<!--允许写设备缓存,用于问题排查-->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />  

<!--允许写入扩展存储,用于写入缓存定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

<!--允许读设备等信息,用于问题排查-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

另外还需要配置定位servcie, 在/android/app/src/main/AndroidManifest.xml添加如下代码配置service:

<application ....>
  <!-- 配置定位Service -->
  <service android:name="com.amap.api.location.APSService"/>
</application>

通过AndroidManifest.xml配置Key:

//开发者申请的key
<meta-data android:name="com.amap.api.v2.apikey" android:value="key" > </meta-data>

通过build.gradle配置:

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))


signingConfigs {
    release {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
    }
    debug {
        keyAlias keystoreProperties['keyAlias']
        keyPassword keystoreProperties['keyPassword']
        storeFile file(keystoreProperties['storeFile'])
        storePassword keystoreProperties['storePassword']
    }   

}
buildTypes {
    debug {
        signingConfig signingConfigs.debug
        minifyEnabled false
        shrinkResources false
    }

    release {
        signingConfig signingConfigs.release
        minifyEnabled false
        shrinkResources false
    }
}
defaultConfig {
    ...
    manifestPlaceholders = [
        AMAP_KEY: "******",
    ]

}



dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    ...
    implementation 'com.amap.api:location:latest.integration'
}

在android/下创建key.propeerties文件:

storePassword=******
keyPassword=******
keyAlias=key
storeFile=key/key.keystore

keystore签名文件:

//  生成keystore签名文件;
keytool -genkey -v -keystore key.keystore -alias key -keyalg RSA -keysize 2048 -validity 10000


//  查看keystore签名文件:
keytool -list -v -keystore key.keystore


//  查看包公钥
keytool -list -rfc --keystore key.keystore  | openssl x509 -inform pem -pubkey


IOS端

根据业务需要,在info.plist文件中,配置相应的定位权限,详细可参考:手动部署-创建工程-开发指南-iOS 定位SDK | 高德地图API

如果需要后台定位功能,需要额外操作配置,详细可参考:后台定位-获取位置-开发指南-iOS 定位SDK | 高德地图API

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

The৲One

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值