You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取Geohash四个顶点的经纬度?优先采用Flutter实现

在Flutter中获取Geohash的四个顶点经纬度

推荐方案:使用第三方Geohash库

直接借助成熟的第三方库可以省去手动解析Geohash编码的复杂逻辑,这里推荐geohash库:

  1. 添加依赖
    在项目的pubspec.yaml中添加依赖:
dependencies:
  geohash: ^2.0.1

执行flutter pub get完成依赖安装。

  1. 实现顶点计算逻辑
    通过库提供的方法解析Geohash得到边界范围,再从边界中提取四个顶点的经纬度:
import 'package:geohash/geohash.dart';

// 传入Geohash字符串,返回四个顶点的经纬度列表(格式:[纬度, 经度])
List<List<double>> getGeohashVertices(String geohash) {
  // 解析Geohash得到经纬度边界
  final bounds = Geohash.decodeBounds(geohash);
  final minLon = bounds[0];
  final minLat = bounds[1];
  final maxLon = bounds[2];
  final maxLat = bounds[3];

  // 返回四个顶点:西南、西北、东南、东北
  return [
    [minLat, minLon],
    [maxLat, minLon],
    [minLat, maxLon],
    [maxLat, maxLon],
  ];
}

// 使用示例
void main() {
  String targetGeohash = 'wx4g0s';
  List<List<double>> vertices = getGeohashVertices(targetGeohash);
  print('四个顶点经纬度:');
  print('西南角:${vertices[0]}');
  print('西北角:${vertices[1]}');
  print('东南角:${vertices[2]}');
  print('东北角:${vertices[3]}');
}

手动实现思路(可选)

如果不想依赖第三方库,核心步骤是:

  • 将Geohash字符转换为二进制串
  • 交替拆分二进制串为经度、纬度对应的二进制部分
  • 根据二进制串计算出经纬度的范围边界
  • 从边界中提取四个顶点
    不过手动实现需要处理大量编码映射和精度计算,容易出错,非必要场景不推荐。

内容的提问来源于stack exchange,提问作者Daniel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 09:44:51