搜索分类、插件与代码仓

插件详情v3.0.2

gmaps_by_road_distance_calculator

这是一个用于计算地图上两点之间道路距离的包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
119
喜欢数
14
Pub 分数
120

安装插件

flutter pub add gmaps_by_road_distance_calculator

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

此 Flutter 包使用 Google 地图数据计算两个地理点之间的基于道路的距离。它通过利用实际的道路网络,实现准确的旅行距离和路线估算,非常适合导航和物流应用。该包支持多种平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,确保广泛的兼容性。开发者可以通过简单的命令行添加轻松集成。它专为需要基于实际道路路径而非直线(如飞鸟般)测量的精确实时距离计算用例而设计。

适合场景

  • 导航应用
  • 物流与配送跟踪
  • 路线规划工具
  • 行程时间估算
  • 基于距离的地图筛选

主要特点

  • 道路距离计算
  • 多平台支持
  • Google 地图集成
  • 简单 API 接口
  • 实时距离更新

适合对象

  • 开发导航功能的开发者
  • 需要精确旅行距离的应用程序
  • 跨平台地图应用
  • 配送和拼车服务
  • 位置感知分析

使用建议

  • 需要互联网连接以进行API调用
  • 依赖于 Google 地图后端的可用性
  • 高请求量可能产生使用费用
  • 仅限谷歌地图提供的道路网络数据

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

gmaps_by_road_distance_calculator

A Dart package for calculating distances along roads and routes using multiple APIs and formats.

Features

  • Calculate distances between geographical points
  • Support for real-time distance tracking
  • Calculate distances along roads using OSRM (Open Source Routing Machine)
  • Multiple travel modes: driving, bicycling, walking, transit
  • Parse and work with GPX and KML route files
  • Decode polyline strings to geographical points

Getting Started

Installation

Add the package to your pubspec.yaml:

dependencies:
  gmaps_by_road_distance_calculator: ^1.0.0

Then run:

flutter pub get
Import
import 'package:gmaps_by_road_distance_calculator/gmaps_by_road_distance_calculator.dart';

Usage

Basic Distance Calculation

Calculate straight-line distances between geographical points:

final calculator = ByRoadDistanceCalculator();
final points = [
  LatLng(37.7749, -122.4194), // San Francisco
  LatLng(34.0522, -118.2437), // Los Angeles
];

// Calculate straight-line distance in kilometers
final distance = calculator.calculateDistance(points);
print('Distance: $distance km');
Calculate Road Distance Using OSRM

Calculate actual road distance between two points using the OSRM service:

final calculator = ByRoadDistanceCalculator();
final start = LatLng(37.7749, -122.4194); // San Francisco
final end = LatLng(34.0522, -118.2437); // Los Angeles

// Calculate driving distance
calculator.calculateDistanceViaOSRM(
  start: start,
  end: end,
  mode: TravelModes.driving,
).then((result) {
  print('Driving distance: ${result.distance} km');
  print('Route consists of ${result.points.length} points');
});

// Calculate walking distance
calculator.calculateDistanceViaOSRM(
  start: start,
  end: end,
  mode: TravelModes.walking,
).then((result) {
  print('Walking distance: ${result.distance} km');
});
Real-time Distance Tracking

Track and update distance in real-time as new points are added:

final calculator = ByRoadDistanceCalculator();

// Listen for distance updates
calculator.distanceUpdates.listen((distance) {
  print('Current distance: $distance km');
});

// Add points as they become available (e.g., from GPS)
calculator.addPoint(LatLng(37.7749, -122.4194));
calculator.addPoint(LatLng(37.7850, -122.4100));
calculator.addPoint(LatLng(37.7950, -122.4000));

// Reset tracking when needed
calculator.clearPoints();
Working with GPX Files

Parse GPX files to extract geographical points:

final calculator = ByRoadDistanceCalculator();
final gpxContent = '''
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1">
  <trk>
    <trkseg>
      <trkpt lat="37.7749" lon="-122.4194"></trkpt>
      <trkpt lat="37.7850" lon="-122.4100"></trkpt>
      <trkpt lat="37.7950" lon="-122.4000"></trkpt>
    </trkseg>
  </trk>
</gpx>
''';

final points = calculator.parseGPX(gpxContent);
final distance = calculator.calculateDistance(points);
print('Distance from GPX: $distance km');
Working with KML Files

Parse KML files to extract geographical points:

final calculator = ByRoadDistanceCalculator();
final kmlContent = '''
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <Placemark>
      <LineString>
        <coordinates>
          -122.4194,37.7749,0
          -122.4100,37.7850,0
          -122.4000,37.7950,0
        </coordinates>
      </LineString>
    </Placemark>
  </Document>
</kml>
''';

final points = calculator.parseKML(kmlContent);
final distance = calculator.calculateDistance(points);
print('Distance from KML: $distance km');
Decode Polyline Strings

Convert encoded polyline strings to geographical points:

final encodedPolyline = "_p~iF~ps|U_ulLnnqC_mqNvxq`@";
final points = ByRoadDistanceCalculator.decodePolylineString(encodedPolyline);

Advanced Configuration

Custom OSRM Server

You can specify a custom OSRM server URL if you're hosting your own instance:

calculator.calculateDistanceViaOSRM(
  start: start,
  end: end,
  mode: TravelModes.driving,
  customOSRMUrl: 'https://your-custom-osrm-server.com/route/v1',
).then((result) {
  print('Distance: ${result.distance} km');
});

Dependencies

This package depends on:

  • latlong2: For geographical coordinate operations
  • http: For API requests
  • xml: For parsing GPX and KML files

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Issues and Feedback

Please file issues and feedback here.

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目

暂无相关数据。