我在pub.dev上没有找到包,所以你必须使用我上面提到的库,直接来自GitHub。它由 Google 提供,因此应该非常安全且维护良好。
为此,您将其添加到pubspec.yaml,如下所示:
dependencies:
flutter:
sdk: flutter
open_location_code:
git:
url: git://github.com/google/open-location-code.git
path: dart
然后,在您的代码中导入库:
import 'package:open_location_code/open_location_code.dart' as olc;
要将 lat&lon 转换为 plus 代码,请执行以下操作(创建 CodeArea 对象):
// Googleplex
// 1600 Amphitheatre Pkwy
// Mountain View, CA 94043
// The coordinates of Google's Global HQ
var codeFromLatLon = olc.encode(37.425562, -122.081812, codeLength: 1000000);
print(codeFromLatLon.toString());
// The above will print '849VCWG9+67GCC32'
要将加号代码解码为纬度,请执行以下操作:
var codeFromPlusCode = olc.decode('849VCWG9+67GCC32');
print(codeFromPlusCode.toString());
// The above will print:
// CodeArea(south:37.425562, west:-122.08181201171875, north:37.42556204, east:-122.08181188964843, codelen: 15)
为了访问纬度和经度,您必须执行以下操作:
print('Lat: ${codeFromPlusCode.center.latitude}, Lon: ${codeFromPlusCode.center.longitude}');
请注意,当您在 plus 代码中编码 lat&lon 对时,您可以将长度限制为 10。这将为您提供更短的代码,但定位不太准确:
var codeFromLatLonShorter = olc.encode(37.425562, -122.081812, codeLength: 10);
print(codeFromLatLonShorter.toString());
// The above will print '849VCWG9+67'
希望以上内容对您有所帮助。这是detailed explanation。