10秒鉴真伪,1.88克拉18K金的闪耀密码

核心内容摘要

爱搞为爱点赞夏天,为心动瞬间加冕
汤芳《美丽的松花江》:一部关于生命、爱与家国情怀的诗意画卷

孙尚香:不止三国,更有闪耀的“正能量”!

Flutter地图与定位开发指南google_maps_flutter与geolocator整合实践引言为什么你的App需要地图与定位如今用户早已习惯用地图查找附近餐厅、用打车软件实时追踪车辆、在社交应用上“打卡”分享位置。

地图与定位能力几乎成了现代移动应用的“标配”。

如果你正在用Flutter开发这类应用那么google_maps_flutter和geolocator这两个插件将是你不可或缺的利器。

本文将带你深入这两个插件的整合使用从基本原理讲起到一步步实现一个功能完整的地图定位示例。

无论你想做出行导航、本地服务推荐还是简单的轨迹记录这里的内容都能帮你快速上手。

核心插件它们是如何工作的

google_maps_flutter在Flutter中嵌入原生地图它是怎么实现的google_maps_flutter底层通过Flutter的平台通道与原生系统通信在Android端封装Google Maps Android API在iOS端封装Google Maps iOS SDK。

地图视图本身通过AndroidView/UiKitView嵌入到Flutter的widget树中因此你能获得原生地图的性能同时享受Flutter UI的开发效率。

一个有趣的细节混合渲染地图底图由原生视图负责渲染而地图上的标记、信息窗等覆盖物则由Flutter widget绘制。

这种混合模式既保证了地图滑动的流畅性又让自定义UI变得简单。

主要能力多种地图类型普通、卫星、混合、地形交互控件指南针、缩放按钮、我的位置按钮等绘制标记、折线、多边形支持地图相机移动动画与手势可通过JSON自定义地图样式

geolocator让定位调用变得简单一致它帮我们解决了什么不同平台Android、iOS、Web的定位API各不相同。

geolocator封装了这些差异提供了一套统一的Dart API。

你不需要关心底层是用的FusedLocationProvider还是Core Location只管调用就行。

精度不是唯一的考量定位精度越高通常意味着越耗电。

geolocator允许你根据场景选择enum LocationAccuracy { lowest, // 功耗最低精度约1公里 low, // 低功耗精度约500米 medium, // 平衡模式精度约100米 high, // 较高精度约10米 best, // 最佳精度约5米 bestForNavigation // 导航专用精度最高但也最耗电 }常用的定位方式获取一次当前位置getCurrentPosition持续监听位置变化getPositionStream可以按距离或时间间隔过滤更新

项目配置一步都不能错

添加依赖在pubspec.yaml中加入dependencies: flutter: sdk: flutter google_maps_flutter: ^

2.

6 geolocator: ^

10.

0 permission_handler: ^

10.

0 # 用于权限申请

平台配置关键步骤Android端(android/app/src/main/AndroidManifest.xml)manifest xmlns:androidhttp://schemas.android.com/apk/res/android !-- 定位权限 -- uses-permission android:nameandroid.permission.ACCESS_FINE_LOCATION/ uses-permission android:nameandroid.permission.ACCESS_COARSE_LOCATION/ !-- 地图需要网络 -- uses-permission android:nameandroid.permission.INTERNET/ application !-- 替换成你在Google Cloud控制台申请的API密钥 -- meta-data android:namecom.google.android.geo.API_KEY android:valueYOUR_ANDROID_API_KEY/ /application /manifestiOS端需要配置两步Info.plist中添加权限描述keyNSLocationWhenInUseUsageDescription/key string需要您的位置信息来提供地图服务/string keyNSLocationAlwaysAndWhenInUseUsageDescription/key string需要持续获取位置来提供导航服务/stringAppDelegate.swift中设置API密钥import GoogleMaps override func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) - Bool { GMSServices.provideAPIKey(YOUR_IOS_API_KEY) // ... 其他代码 }提示iOS模拟器上需要手动设置模拟位置Features → Location真机调试则需在设置中授予位置权限。

完整实现从权限到地图交互

权限处理服务在实际获取位置前必须处理好权限申请。

这里封装了一个简单的服务类class LocationPermissionService { /// 检查并申请定位权限 static Futurebool checkAndRequestPermission() async { // 先检查系统定位服务是否开启 bool serviceEnabled await Geolocator.isLocationServiceEnabled(); if (!serviceEnabled) { // 可以在这里提示用户去系统设置中打开 return false; } LocationPermission permission await Geolocator.checkPermission(); if (permission LocationPermission.deniedForever) { // 被永久拒绝只能引导用户去设置页手动开启 return false; } if (permission LocationPermission.denied) { // 首次申请或临时拒绝再次请求 permission await Geolocator.requestPermission(); return permission LocationPermission.whileInUse || permission LocationPermission.always; } return true; } }

核心地图页面下面这个MapLocationScreen类实现了地图展示、实时定位、添加标记、轨迹绘制等核心功能。

代码较长我们分段来看关键部分。

状态初始化与定位启动class _MapLocationScreenState extends StateMapLocationScreen { final CompleterGoogleMapController _mapController Completer(); final SetMarker _markers {}; final SetPolyline _polylines {}; LatLng? _currentPosition; bool _isLoading true; String _locationInfo 正在获取位置...; override void initState() { super.initState(); _initializeLocation(); // 应用启动后立即初始化定位 } Futurevoid _initializeLocation() async { bool hasPermission await LocationPermissionService.checkAndRequestPermission(); if (!hasPermission) { setState(() { _locationInfo 位置权限被拒绝; _isLoading false; }); return; } await _getCurrentLocation(); // 获取一次当前位置 _startLocationStream(); // 开始持续监听 }获取当前位置并移动地图视角Futurevoid _getCurrentLocation() async { try { Position position await Geolocator.getCurrentPosition( desiredAccuracy: LocationAccuracy.high, timeLimit: const Duration(seconds:

, ); setState(() { _currentPosition LatLng(position.latitude, position.longitude); _locationInfo 纬度: ${position.latitude.toStringAsFixed(

} 经度: ${position.longitude.toStringAsFixed(

} 精度: ${position.accuracy?.toStringAsFixed(

?? N/A}米; _isLoading false; _addUserMarker(); // 在地图上添加代表用户位置的标记 }); // 将地图视角移动到当前位置 _moveToCurrentLocation(); } on TimeoutException { setState(() { _locationInfo 获取位置超时; _isLoading false; }); } catch (e) { setState(() { _locationInfo 获取位置失败: $e; _isLoading false; }); } } Futurevoid _moveToCurrentLocation() async { if (_currentPosition null) return; final controller await _mapController.future; await controller.animateCamera( CameraUpdate.newCameraPosition( CameraPosition(target: _currentPosition!, zoom: 16, tilt:

, ), ); }持续监听位置变化如导航场景void _startLocationStream() { const LocationSettings locationSettings LocationSettings( accuracy: LocationAccuracy.high, distanceFilter: 10, // 每移动10米更新一次 ); Geolocator.getPositionStream(locationSettings: locationSettings) .listen((Position position) { if (mounted) { setState(() { _currentPosition LatLng(position.latitude, position.longitude); _updateUserMarker(); // 更新地图上用户标记的位置 }); } }); }地图UI构建override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text(Flutter地图与定位), actions: [ IconButton( icon: const Icon(Icons.my_location), onPressed: _moveToCurrentLocation, tooltip: 回到当前位置, ), ], ), body: Stack( children: [ GoogleMap( initialCameraPosition: const CameraPosition( target: LatLng(

3

9042,

116.

, // 默认北京 zoom: 14, ), onMapCreated: (controller) _mapController.complete(controller), markers: _markers, polylines: _polylines, myLocationEnabled: true, // 显示蓝点 zoomControlsEnabled: true, onTap: (LatLng position) { // 点击地图添加标记 _addPointOfInterest(position, 新标记, 点击添加的点); }, ), if (_isLoading) const Center(child: CircularProgressIndicator()), // 顶部位置信息卡片 Positioned( top: 16, left: 16, right: 16, child: Card( child: Padding( padding: const EdgeInsets.all(

, child: Text(_locationInfo), ), ), ), ], ), floatingActionButton: FloatingActionButton.extended( onPressed: _moveToCurrentLocation, icon: const Icon(Icons.navigation), label: const Text(回到我的位置), ), ); }

进阶功能让地图更智能

自定义地图样式你可以通过JSON完全改变地图的视觉风格比如实现深色模式String _darkMapStyle [ { elementType: geometry, stylers: [{color: #212121}] }, { elementType: labels.icon, stylers: [{visibility: off}] } ] ; Futurevoid _setMapStyle() async { final controller await _mapController.future; await controller.setMapStyle(_darkMapStyle); }

地理编码与逆地理编码“地理编码”指的是将地址转换为坐标“逆地理编码”则是将坐标转换为具体地址。

这需要用到geocoding插件import package:geocoding/geocoding; // 地址转坐标 FutureLatLng? addressToLatLng(String address) async { try { ListLocation locations await locationFromAddress(address); if (locations.isNotEmpty) { return LatLng(locations.first.latitude, locations.first.longitude); } } catch (e) { debugPrint(地理编码失败: $e); } return null; } // 坐标转地址 FutureString? latLngToAddress(LatLng position) async { try { ListPlacemark placemarks await placemarkFromCoordinates( position.latitude, position.longitude, ); if (placemarks.isNotEmpty) { Placemark p placemarks.first; return ${p.locality} ${p.street}; // 例如“北京市海淀区中关村大街” } } catch (e) { debugPrint(逆地理编码失败: $e); } return null; }

轨迹记录如果你需要记录用户的移动路径如运动类App可以这样实现class TrackRecorder { final ListPosition _trackPoints []; void addPoint(Position position) { _trackPoints.add(position); } Polyline buildTrackLine() { return Polyline( polylineId: const PolylineId(user_track), points: _trackPoints.map((p) LatLng(p.latitude, p.longitude)).toList(), color: Colors.green, width: 3, ); } void clear() { _trackPoints.clear(); } }

性能优化与

常见问题

地图性能标记太多考虑使用聚类插件如google_maps_clustering当缩放级别改变时将相邻的标记聚合显示。

按需加载只加载当前地图视野范围内的覆盖物可以通过controller.getVisibleRegion()获取视野边界。

定位策略优化根据应用状态动态调整定位精度可以显著节省电量LocationAccuracy getAppropriateAccuracy(AppState state) { switch (state) { case AppState.background: return LocationAccuracy.low; // 后台低精度 case AppState.foreground: return LocationAccuracy.medium; // 前台中等精度 case AppState.navigation: return LocationAccuracy.bestForNavigation; // 导航时最高精度 default: return LocationAccuracy.high; } }

常见错误处理定位过程中难免遇到问题给用户明确的反馈很重要String handleLocationError(dynamic error) { if (error is PermissionDeniedException) { return 位置权限被拒绝请在设置中开启; } else if (error is LocationServiceDisabledException) { return 位置服务未启用请开启GPS; } else if (error is TimeoutException) { return 获取位置超时请检查网络连接; } else { return 位置服务错误: ${error.toString()}; } }结语google_maps_flutter与geolocator的组合为Flutter开发者提供了一套强大且易用的地图定位解决方案。

从基本的显示地图、获取位置到高级的轨迹记录、地理编码这两个插件都能很好地覆盖。

实际开发中建议根据具体场景调整定位精度和更新频率在功能与功耗之间找到平衡。

如果遇到权限问题或地图不显示请逐一检查API密钥配置和平台权限设置——这两步往往是初学者最容易出错的地方。

希望这篇指南能帮你顺利实现Flutter中的地图与定位功能。

如果有任何问题或更优的实现方式欢迎在评论区交流讨论。

t大校花沉浮录续集TXT-t大校花沉浮录续集应用

百度百家号客服电话人工服务

123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123 123