如何获取Geohash四个顶点的经纬度?优先采用Flutter实现
在Flutter中获取Geohash的四个顶点经纬度
推荐方案:使用第三方Geohash库
直接借助成熟的第三方库可以省去手动解析Geohash编码的复杂逻辑,这里推荐geohash库:
- 添加依赖
在项目的pubspec.yaml中添加依赖:
dependencies: geohash: ^2.0.1
执行flutter pub get完成依赖安装。
- 实现顶点计算逻辑
通过库提供的方法解析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
相关产品推荐
相关产品推荐

