Flutter地理位置获取插件latlong的使用
Flutter地理位置获取插件latlong的使用
latlong
是一个轻量级的 Dart 库,用于常见的经纬度计算。该库支持两种算法:“Haversine” 和 “Vincenty”。其中,“Haversine” 算法稍快,而 “Vincenty” 算法则更加精确!
Catmull-Rom
算法用于平滑路径。
基本用法
距离计算
final Distance distance = new Distance();
// 计算两地之间的距离(单位为千米)
final int km = distance.as(LengthUnit.Kilometer,
new LatLng(52.518611, 13.408056), new LatLng(51.519475, 7.46694444));
// 输出结果:423 千米
print(km);
// 计算两地之间的距离(单位为米)
final int meter = distance(
new LatLng(52.518611, 13.408056),
new LatLng(51.519475, 7.46694444)
);
// 输出结果:422591.551 米
print(meter);
偏移计算
final Distance distance = const Distance();
final num distanceInMeter = (EARTH_RADIUS * math.PI / 4).round();
final p1 = new LatLng(0.0, 0.0);
final p2 = distance.offset(p1, distanceInMeter, 180);
// 输出结果:纬度为 -45.219848,经度为 0.0 的点
print(p2.round());
// 输出结果:45° 13' 11.45" S, 0° 0' 0.00" O
print(p2.toSexagesimal());
路径平滑
// zigzag 是一系列坐标点的列表
final Path path = new Path.from(zigzag);
// 平滑后的路径
final Path steps = path.equalize(8, smoothPath: true);
// 平滑路径效果

特性和问题
如需提交功能请求或报告问题,请前往 GitHub 问题跟踪器。
许可证
Copyright 2015 Michael Mitterer (office@mikemitterer.at),
IT-Consulting and Development Limited, Austrian Branch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language
governing permissions and limitations under the License.
1 回复
更多关于Flutter地理位置获取插件latlong的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
在Flutter中,latlong
是一个用于处理地理坐标(纬度和经度)的库,但它本身并不提供获取设备地理位置的功能。要获取设备的地理位置,你通常需要使用 geolocator
插件。latlong
主要用于处理坐标计算、距离计算等操作。
下面是如何结合使用 geolocator
和 latlong
来获取设备地理位置并进行一些基本操作的步骤:
1. 添加依赖
首先,在你的 pubspec.yaml
文件中添加 geolocator
和 latlong
依赖:
dependencies:
flutter:
sdk: flutter
geolocator: ^9.0.2
latlong: ^0.9.0
然后运行 flutter pub get
来安装这些依赖。
2. 获取设备地理位置
使用 geolocator
来获取设备的当前位置:
import 'package:geolocator/geolocator.dart';
Future<Position> getCurrentLocation() async {
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// 位置服务未启用,提示用户启用
return Future.error('Location services are disabled.');
}
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
// 权限被拒绝,提示用户
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// 权限被永久拒绝,提示用户
return Future.error('Location permissions are permanently denied, we cannot request permissions.');
}
// 获取当前位置
return await Geolocator.getCurrentPosition();
}
3. 使用 latlong
处理坐标
获取到位置后,你可以使用 latlong
来处理坐标:
import 'package:latlong/latlong.dart';
void main() async {
// 获取当前位置
Position position = await getCurrentLocation();
// 使用 latlong 创建 LatLng 对象
LatLng currentLocation = LatLng(position.latitude, position.longitude);
// 示例:计算两个坐标之间的距离
LatLng anotherLocation = LatLng(37.7749, -122.4194); // 例如:旧金山
double distance = Distance().as(LengthUnit.Kilometer, currentLocation, anotherLocation);
print('Current Location: $currentLocation');
print('Distance to San Francisco: $distance km');
}