Flutter 项目实战 高德定位计算距离并展示首页数据 (六)

/  注册高德地图 (安卓)  /

Android  Studio 创建签名文件

Android 应用签名

Signed APK

Generate Signed Bundle or Apk

 创建密钥 (Create New Key Store)  签署您的应用程序

 创建签名

 密钥

 证书

 创建密钥并存储到制定的路径 

密钥创建内容

创建应用并生成key

注册高德地图开发者账户 Android 如何获取 SHA1 值?

/  获取定位信息  / 

集成高德定位Flutter插件 

打开 pubspec.yaml 文件 添加 amap_flutter_location (高德定位)permission_handler (应用权限)、app_settings (app权限设置) location_service_check (是否开通gps) 插件依赖配置

amap_flutter_location: any # 高德Flutter定位插件
permission_handler: any #应用权限
app_settings: any #app权限设置
location_service_check: any #是否开通GPS

 判断是否开启定位权限

/// 申请定位权限
/// 授予定位权限返回true, 否则返回false
Future<bool> requestLocationPermission() async {
  //获取当前的权限
  var status = await Permission.location.status;
  if (status == PermissionStatus.granted) {
    //已经授权
    return true;
  } else {
    //未授权则发起一次申请
    status = await Permission.location.request();
    if (status == PermissionStatus.granted) {
      return true;
    } else {
      return false;
    }
  }
}

 判断是否开启GPS

...

 bool _isOpenGps = await LocationServiceCheck.checkLocationIsOpen;
    if(!_isOpenGps) {
      logV("定位权限申请通过");
      AppSettings.openLocationSettings();
    }

...

动态申请定位权限

/// 动态申请定位权限
Future<bool> requestPermission() async {
  // 申请权限
  bool hasLocationPermission = await requestLocationPermission();
  if (hasLocationPermission) {
    bool _isOpenGps = await LocationServiceCheck.checkLocationIsOpen;
    if(!_isOpenGps) {
      logV("定位权限申请通过");
      AppSettings.openLocationSettings();
    }
    return _isOpenGps;
  } else {
    logV("定位权限申请不通过");
    // 跳转到系统设置页
    AppSettings.openAppSettings();
  }
  return hasLocationPermission;
}

高德地图初始化 

StreamSubscription<Map<String, Object>>? _locationListener;
AMapFlutterLocation? _locationPlugin = AMapFlutterLocation();
void amapLocaitonInit(IMapLocationResultCallBack? locationResultCallBack) {
  AMapFlutterLocation.updatePrivacyShow(true, true);
  AMapFlutterLocation.updatePrivacyAgree(true);
  AMapFlutterLocation.setApiKey("c9856efd6bae11ff6897cffede9af428", "");

  ///iOS 获取native精度类型
  if (Platform.isIOS) {
    requestAccuracyAuthorization();
  }
  ///注册定位结果监听
  _locationListener = _locationPlugin!
      .onLocationChanged()
      .listen((Map<String, Object>? result) {
    locationResultCallBack!(result);
  });
}

开始定位

///开始定位
void startLocation() async {
  /// 检查是否开启GSP和定位权限
  bool hasLocationPermission = await requestPermission();
  if (hasLocationPermission) {
    _setLocationOption();
    _locationPlugin!.startLocation();
  }
}

停止定位 

为了让定位不一直执行,当获取到定位数据时可以停止定位

///停止定位
void stopLocation() {
  _locationPlugin!.stopLocation();
}

销毁定位 

void amapLocationDispose() {
  ///移除定位监听
  if (null != _locationListener) {
    _locationListener!.cancel();
  }

  ///销毁定位
  if (null != _locationPlugin) {
    _locationPlugin!.destroy();
  }
}

/  定位数据创建MySQL表  / 

高德地图定位获取到的数据转换成Json字符串并用创建的.json文件保存起来,方便使用Navicat Preminum 生成表 。

 

 

 

 

 /  个人位置信息插入、修改  /

创建LocationMapper.xml

里面存放了SQL查询的语句 (插入用户位置信息 、根据userId查询用户位置信息 、修改用户位置信息)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xm.chat.dao.LocationMapper">

    <resultMap type="com.xm.chat.entity.admin.Location" id="BaseResultMap">
        <result property="callbackTime" column="callbackTime"/>
        <result property="locationTime" column="locationTime"/>
        <result property="locationType" column="locationType"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="accuracy" column="accuracy"/>
        <result property="altitude" column="altitude"/>
        <result property="bearing" column="bearing"/>
        <result property="speed" column="speed"/>
        <result property="country" column="country"/>
        <result property="province" column="province"/>
        <result property="city" column="city"/>
        <result property=
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

️ 邪神

你自己看着办,你喜欢打赏我就赏

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

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

打赏作者

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

抵扣说明:

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

余额充值