精选文章

Flutter 地图

2025-04-04 · 组件

地图通常分两层:定位地图展示。本文先覆盖定位与地理编码(如果需要地图 SDK,可再扩展)。

0. 依赖

dependencies:
  geolocator: ^10.1.0
  geocode: ^1.0.3

1. 获取定位

Future<Position> getPosition() async {
  final serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) throw Exception('定位服务未开启');
  return Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
}

2. 地理编码(经纬度 -> 地址)

final geocode = GeoCode();
final address = await geocode.reverseGeocoding(
  latitude: pos.latitude,
  longitude: pos.longitude,
);

3. 常见坑点

  • 权限未处理:定位直接失败
  • 地理编码限流:注意频率限制

4. 实践清单

  • 权限检查与兜底
  • 定位失败处理
  • 地理编码节流

JJ

作者简介

专注于内容创作、产品策略与设计实践。欢迎交流合作。

上一篇 下一篇