flutter高德地图大头针

1、效果图
在这里插入图片描述
2、pub get

#地图定位
amap_flutter_map: ^3.0.0
amap_flutter_location: ^3.0.0

3、上代码

import 'dart:async';
import 'dart:io';import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';import 'package:amap_flutter_map/amap_flutter_map.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../utils/loading.dart';
import '../../Home/map/canteen.dart';class AMapPage extends StatelessWidget {final String title;final String iosKey;final String androidKey;final LatLng? latLng;final void Function(AMapController controller)? onMapCreated;AMapPage(this.title,this.iosKey, this.androidKey, {Key? key, this.latLng, this.onMapCreated}) : super(key: key);// 添加线段//lines 结构   [LatLng(26.642386, 106.65195799999998), LatLng(26.641542, 106.65062999999998), LatLng(26.641402, 106.65063499999997), LatLng(26.641411, 106.65086300000002), LatLng(26.641334, 106.65067099999999), LatLng(26.641374, 106.650688), LatLng(26.641567, 106.65078499999998), LatLng(26.641656, 106.651207), LatLng(26.641708, 106.651408), LatLng(26.641413, 106.65066100000001), LatLng(26.641484, 106.65012100000001)]//定义@overrideWidget build(BuildContext context) {List<Marker> markers = [Marker(position: const LatLng(39.909215, 116.41005),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon1.png"),infoWindow: const InfoWindow(title: '东城区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.9305, 116.36003),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon2.png"),infoWindow: const InfoWindow(title: '西城区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.9484, 116.48548),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon3.png"),infoWindow: const InfoWindow(title: '朝阳区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.8585, 116.28625),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon4.png"),infoWindow: const InfoWindow(title: '丰台区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.90564, 116.2229),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon5.png"),infoWindow: const InfoWindow(title: '石景山区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.95931, 116.29812),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon1.png"),infoWindow: const InfoWindow(title: '海淀区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.94043, 116.10137),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon2.png"),infoWindow: const InfoWindow(title: '门头沟区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.74786, 116.14257),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon3.png"),infoWindow: const InfoWindow(title: '房山区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(39.90564, 116.2229),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon4.png"),infoWindow: const InfoWindow(title: '石景山区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.65716, 39.90966),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon5.png"),infoWindow: const InfoWindow(title: '通州区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.65417, 40.1302),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon1.png"),infoWindow: const InfoWindow(title: '顺义区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.2312, 40.22072),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon3.png"),infoWindow: const InfoWindow(title: '昌平区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.34149, 39.72668),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon4.png"),infoWindow: const InfoWindow(title: '大兴区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.63168, 40.31602),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon5.png"),infoWindow: const InfoWindow(title: '怀柔区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(117.12133, 40.14056),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon1.png"),infoWindow: const InfoWindow(title: '平谷区',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(116.84295, 40.37618),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon2.png"),infoWindow: const InfoWindow(title: '密云县',snippet: '',),onTap: (marker){},),Marker(position: const LatLng(115.97494, 40.45672),icon:BitmapDescriptor.fromIconPath("images/ic_mapIcon3.png"),infoWindow: const InfoWindow(title: '延庆县',snippet: '',),onTap: (marker){},),];final size = MediaQuery.of(context).size;CameraPosition kInitialPosition = CameraPosition(target: latLng ?? const LatLng(39.909187, 116.397451),zoom: 10.0,);return Scaffold(appBar: title.isNotEmpty ? AppBar(title: Text(title,style: const TextStyle(color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),),// title:title,elevation: 0,backgroundColor: Colors.white,) :null,backgroundColor: ThemeColor.sendMsgColor,body: AMapWidget(initialCameraPosition: kInitialPosition,buildingsEnabled: false,onMapCreated: onCreated,privacyStatement: const AMapPrivacyStatement(hasShow: true, hasAgree: true, hasContains: true),apiKey: AMapApiKey(iosKey: iosKey,androidKey: androidKey,),markers:Set<Marker>.of(markers),// Set<marker>.of(markers.values),onTap: (marker){},onPoiTouched: (marker){print('object');},),);}/// 高德地图static Future<bool> gotoGaoDeMap(keywords,longitude, latitude) async {var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';bool canLaunchUrl = await canLaunch(url);if (!canLaunchUrl) {toast('未检测到高德地图~');return false;}await launch(url);return true;}/// 腾讯地图static Future<bool> gotoTencentMap(longitude, latitude) async {var url = 'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';bool canLaunchUrl = await canLaunch(url);if (!canLaunchUrl) {toast('未检测到腾讯地图~');// ToastUtil.show('未检测到腾讯地图~');return false;}await launch(url);return canLaunchUrl;}void onCreated(AMapController controller) {AMapApprovalNumber.setApprovalNumber(controller);if (onMapCreated != null) onMapCreated!(controller);}}/// 获取审图号
/// 这里设计的很奇怪,当地图创建后才知道这个号码,但是这个号码不一定要显示在地图之上,却一定要显示在app之内,主要是和上架后的合规有关
class AMapApprovalNumber {static String? mapContentApprovalNumber;static String? satelliteImageApprovalNumber;static Function(String? mapContentApprovalNumber, String? satelliteImageApprovalNumber)? _listener;static void addListener(Function(String? mapContentApprovalNumber, String? satelliteImageApprovalNumber) run) {_listener = run;}static void setApprovalNumber(AMapController? mapController) async {//普通地图审图号mapContentApprovalNumber = await mapController?.getMapContentApprovalNumber();//卫星地图审图号satelliteImageApprovalNumber = await mapController?.getSatelliteImageApprovalNumber();if (kDebugMode) {print('地图审图号(普通地图): $mapContentApprovalNumber');print('地图审图号(卫星地图): $satelliteImageApprovalNumber');}if (_listener != null) _listener!(mapContentApprovalNumber, satelliteImageApprovalNumber);}
}///需要在程序启动时向用户展示隐私政策等
///高德地图的定位插件,可以进行连续定位,返回当前位置的经纬度以及转译过的位置信息
mixin AMapLocationStateMixin<WIDGET extends StatefulWidget> on State<WIDGET> {String get title;String get iosKey;String get androidKey;/// 是否拥有定位权限bool get hasLocationPermission => _hasLocationPermission;///获取到的定位信息Map<String, Object> get locationResult => _locationResult ?? {};///整理过的数据LocationInfo get locationInfo => LocationInfo(locationResult);///开始定位void startLocation() {///开始定位之前设置定位参数_setLocationOption();_locationPlugin.startLocation();}///停止定位void stopLocation() {_locationPlugin.stopLocation();}Map<String, Object>? _locationResult;StreamSubscription<Map<String, Object>>? _locationListener;final AMapFlutterLocation _locationPlugin = AMapFlutterLocation();// final List<LatLng> _traackPath = [];late AMapController _mapController;@overridevoid initState() {super.initState();setPath();AMapFlutterLocation.updatePrivacyShow(true, true);AMapFlutterLocation.updatePrivacyAgree(true);/// 动态申请定位权限_requestLocationPermission();AMapFlutterLocation.setApiKey(androidKey, iosKey);///iOS 获取native精度类型if (Platform.isIOS) {_requestAccuracyAuthorization();}///注册定位结果监听_locationListener = _locationPlugin.onLocationChanged().listen((Map<String, Object> result) {setState(() {_locationResult = result;});});}// AMapController _mapController;Map mapMakerListMap = <dynamic, Marker>{};Map mapPolylineListMap = <dynamic, Polyline>{};final List<LatLng> _trackPath = [];late final List trackList;late final List<LatLng> pointList;late final LatLng center;late final double zoom;// 设置轨迹路径setPath() {if (trackList.isNotEmpty) {for (var item in trackList) {if (item is Map<String, dynamic>) {LatLng? latLng = LatLng.fromJson(item);if (latLng != null) {_trackPath.add(latLng);}}if (item is List && item.length == 2) {LatLng latLng = LatLng(item[0], item[1]);_trackPath.add(latLng);}}}mapClean();}mapClean() async {mapMakerListMap.clear();mapPolylineListMap.clear();setState(() => null);}@overridevoid dispose() {super.dispose();///移除定位监听if (null != _locationListener) {_locationListener?.cancel();}///销毁定位_locationPlugin.destroy();}///设置定位参数void _setLocationOption() {AMapLocationOption locationOption = AMapLocationOption();///是否单次定位locationOption.onceLocation = false;///是否需要返回逆地理信息locationOption.needAddress = true;///逆地理信息的语言类型locationOption.geoLanguage = GeoLanguage.DEFAULT;locationOption.desiredLocationAccuracyAuthorizationMode = AMapLocationAccuracyAuthorizationMode.ReduceAccuracy;locationOption.fullAccuracyPurposeKey = "AMapLocationScene";///设置Android端连续定位的定位间隔locationOption.locationInterval = 2000;///设置Android端的定位模式<br>///可选值:<br>///<li>[AMapLocationMode.Battery_Saving]</li>///<li>[AMapLocationMode.Device_Sensors]</li>///<li>[AMapLocationMode.Hight_Accuracy]</li>locationOption.locationMode = AMapLocationMode.Hight_Accuracy;///设置iOS端的定位最小更新距离<br>locationOption.distanceFilter = -1;///设置iOS端期望的定位精度/// 可选值:<br>/// <li>[DesiredAccuracy.Best] 最高精度</li>/// <li>[DesiredAccuracy.BestForNavigation] 适用于导航场景的高精度 </li>/// <li>[DesiredAccuracy.NearestTenMeters] 10米 </li>/// <li>[DesiredAccuracy.Kilometer] 1000米</li>/// <li>[DesiredAccuracy.ThreeKilometers] 3000米</li>locationOption.desiredAccuracy = DesiredAccuracy.Best;///设置iOS端是否允许系统暂停定位locationOption.pausesLocationUpdatesAutomatically = false;///将定位参数设置给定位插件_locationPlugin.setLocationOption(locationOption);}///获取iOS native的accuracyAuthorization类型Future<AMapAccuracyAuthorization> _requestAccuracyAuthorization() async {AMapAccuracyAuthorization currentAccuracyAuthorization = await _locationPlugin.getSystemAccuracyAuthorization();if (kDebugMode) {if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationFullAccuracy) {print("精确定位类型");} else if (currentAccuracyAuthorization == AMapAccuracyAuthorization.AMapAccuracyAuthorizationReducedAccuracy) {print("模糊定位类型");} else {print("未知定位类型");}}return currentAccuracyAuthorization;}bool _hasLocationPermission = false;/// 申请定位权限Future<void> _requestLocationPermission() async {//获取当前的权限var status = await Permission.location.status;if (status == PermissionStatus.granted) {//已经授权_hasLocationPermission = true;} else {//未授权则发起一次申请status = await Permission.location.request();if (status == PermissionStatus.granted) {_hasLocationPermission = true;} else {_hasLocationPermission = false;}}if (kDebugMode) {if (_hasLocationPermission) {print("定位权限申请通过");} else {print("定位权限申请不通过");}}}
}class LocationInfo {//TODO:应当再此类对信息做转换,明确数据类型String? locTime;String? province;String? callbackTime;String? district;double? speed;double? latitude;double? longitude;String? country;String? city;String? cityCode;String? street;String? streetNumber;String? address;String? description;double? bearing;double? accuracy;String? adCode;double? altitude;int? locationType;LocationInfo(Map<String, Object> locationResult) {locTime = locationResult["locTime"] as String;province = locationResult["province"] as String;callbackTime = locationResult["callbackTime"] as String;district = locationResult["district"] as String;speed = locationResult["speed"] as double;latitude = double.parse(locationResult["latitude"] as String);longitude = double.parse(locationResult["longitude"] as String);country = locationResult["country"] as String;city = locationResult["city"] as String;print(city);cityCode = locationResult["cityCode"] as String;street = locationResult["street"] as String;streetNumber = locationResult["streetNumber"] as String;address = locationResult["address"] as String;description = locationResult["description"] as String;bearing = locationResult["bearing"] as double;accuracy = locationResult["accuracy"] as double;adCode = locationResult["adCode"] as String;altitude = locationResult["altitude"] as double;locationType = locationResult["locationType"] as int;}}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/111983.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用变压器实现德-英语言翻译【02/8】: 位置编码

一、说明 本文是“用变压器实现德-英语言翻译”系列的第二篇。它从头开始引入位置编码。然后&#xff0c;它解释了 PyTorch 如何实现位置编码。接下来是变压器实现。 二、技术背景 位置编码用于为序列中的每个标记或单词提供相对位置。阅读句子时&#xff0c;每个单词都依赖于它…

git 操作

merge操作 git checkout -b bugFix git commit //在bugFix branch上操作&#xff0c;并且commit git checkout main git commit //在main上操作&#xff0c;并且commit git merge bugFix //此时on main branch&#xff0c;如果在bugFix branch执行merge bugFix&#…

fastadmin think-queue supervisor配置

起因是微信支付回调需要同时做发货处理&#xff0c;但是发货接口不能影响,需要队列进行异步处理1. 1.fastadmin 后台购买queue插件(基于think-queue消息队列) 2.代码 2.1 添加文件&#xff1a;application---->extra--->queue.php 内容&#xff1a;我这里用的数据库做…

leetcode 42. 接雨水

2023.8.29 本题可以用双指针做&#xff0c;求出每一列能盛的雨水&#xff0c;再相加即可。不过暴力法会超时&#xff0c;需要优化。 双指针&#xff08;暴力&#xff09;&#xff1a; class Solution { public:int trap(vector<int>& height) {int ans 0;for(int …

(牛客周赛 9)C.小美的01串翻转

题目&#xff1a; 样例&#xff1a; 输入 10001 输出 8 思路&#xff1a; 这里是连续的找子串&#xff0c;权值的意思是 我们取反操作了多少次&#xff0c; 我们有假设长度是 5 &#xff0c;字符串是 10001 那么相邻不一样的字符串有两种情况 01010 或者 10101&#xf…

高等职业学校物联网实训室建设方案

一、概述 1.1专业背景 物联网&#xff08;Internet of Things&#xff09;被称为继计算机、互联网之后世界信息产业第三次浪潮&#xff0c;它并非一个全新的技术领域&#xff0c;而是现代信息技术发展到一定阶段后出现的一种聚合性应用与技术提升&#xff0c;是随着传感网、通…

耐世特Nexteer EDI解决方案

耐世特Nexteer曾经为美国通用汽车全资子公司&#xff0c;是一家集研发、制造、销售于一体的全球化集团公司。耐世特汽车系统公司是转向系统及相关先进技术的全球供应商。该公司为60多家汽车制造商设计、制造、销售电动助力转向器、液压助力转向器、转向管柱和传动轴产品&#x…

GNU make系列之写Makefile文件(1)

一.欢迎来到我的酒馆 在本章节介绍如何写Makefile文件。 目录 一.欢迎来到我的酒馆二.Makefile包含了什么三.引入其它的Makefile文件四.MAKEFILES变量 二.Makefile包含了什么 2.1 Makefile包含了5种类型&#xff1a;显式的规则&#xff0c;隐式的规则&#xff0c;变量的定义&am…

数据结构:八种数据结构大全

数据结构 1.1 数据结构概述 数据结构是计算机存储、组织数据的方式&#xff1b;通常情况下&#xff0c;精心选择的数据结构可以带来更高的运行或者存储效率。数据结构的优良将直接影响着我们程序的性能&#xff1b;常用的数据结构有&#xff1a;数组&#xff08;Array&#xff…

【Qt学习】05:自定义封装界面类

OVERVIEW 自定义封装界面类1.QListWidget2.QTreeWidget3.QTableWidget4.StackedWidget5.Others6.自定义封装界面类-显示效果&#xff08;1&#xff09;添加设计师界面类&#xff08;2&#xff09;在ui中设计自定义界面&#xff08;3&#xff09;在需要使用的界面中添加&#xf…

面试题(三)

目录 一.Spring 1.Spring IOC & AOP 2.Spring bean (1) 作用域 (2) Spring 中的 bean ⽣命周期 (3) Spring 框架中⽤到了哪些设计模式 二.Mybatis 1.标签 2.Dao接口 3.返回与映射 4.延迟加载 三.Kafka 四.设计模式 1.IO 设计模式 2.Spring 中的设计模式详解…

【前端】常用功能合集

目录 js跳转到新标签打开PDF文件js每十个字符换行 es6用表达式或变量名作为对象的属性名 vuev-for插值、:style、:class父组件加载完后再加载子组件keep-alive缓存跨域请求第三方接口跨域请求之callback&#xff08;不建议&#xff09;读取本地文件浏览器播放提示音audio jquer…

Lora升级!ReLoRa!最新论文 High-Rank Training Through Low-Rank Updates

目录 摘要1 引言2 相关工作3 方法4 实验5 结果6 结论7 局限性和未来工作 关注公众号TechLead&#xff0c;分享AI与云服务技术的全维度知识。作者拥有10年互联网服务架构、AI产品研发经验、团队管理经验&#xff0c;同济本复旦硕&#xff0c;复旦机器人智能实验室成员&#xff0…

1、[春秋云镜]CVE-2022-32991

文章目录 一、相关信息二、解题思路&#xff08;手注&#xff09;三、通关思路&#xff08;sqlmap&#xff09; 一、相关信息 靶场提示&#xff1a;该CMS的welcome.php中存在SQL注入攻击。 NVD关于漏洞的描述&#xff1a; 注入点不仅在eid处&#xff01;&#xff01;&#xff…

路由器的简单概述(详细理解+实例精讲)

系列文章目录 华为数通学习&#xff08;4&#xff09; 目录 系列文章目录 华为数通学习&#xff08;4&#xff09; 前言 一&#xff0c;网段间通信 二&#xff0c;路由器的基本特点 三&#xff0c;路由信息介绍 四&#xff0c;路由表 五&#xff0c;路由表的来源有哪些…

新能源汽车动力总成系统及技术

需要动力系统总成的请联&#xff1a;shbinzer 拆车邦 需要动力系统总成的请联&#xff1a;shbinzer 拆车邦 需要动力系统总成的请联&#xff1a;shbinzer 拆车邦 需要动力系统总成的请联&#xff1a;shbinzer 拆车邦 需要动力系统总成的请联&#xff1a;shbinzer …

k3s or RKE2 helm安装报错dial tcp 127.0.0.1:8080: connect: connection refused

1.报错&#xff1a; Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get "http://127.0.0.1:8080/version": dial tcp 127.0.0.1:8080: connect: connection refused 2.问题原因&#xff1a; 1.因为helm默认使用k8s的配置文件&#xff0c;默…

uniapp 配置网络请求并使用请求轮播图

由于平台的限制&#xff0c;小程序项目中不支持 axios&#xff0c;而且原生的 wx.request() API 功能较为简单&#xff0c;不支持拦截器等全局定制的功能。因此&#xff0c;建议在 uni-app 项目中使用 escook/request-miniprogram 第三方包发起网络数据请求。 官方文档&#xf…

宏观经济和风电预测误差分析(Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

springboot1.5.12升级至2.6.15

首先&#xff0c;加入springboot升级大版本依赖&#xff0c;会在升级过程中打印出错日志提示&#xff08;升级完毕可去除&#xff09; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-properties-migrator</art…