搜尋套件、儲存庫與 APP 分類

套件詳情v2.5.0

gpx

用於載入、操作和儲存 GPX 格式 GPS 數據的套件(一種輕量級 XML 數據格式,用於交換 GPS 數據——航點、路線和軌跡)。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
2.3萬
喜歡數
67
Pub 分數
160

安裝套件

flutter pub add gpx

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

gpx 套件讓 Flutter 開發者能夠載入、操作和儲存 GPX 格式的 GPS 資料——一種輕量級基於 XML 的標準,用於交換航點、路線和軌跡。它支援在所有主要平台讀取和寫入 GPX 檔案,非常適合需要 GPS 資料互通性的地理定位應用。該套件提供乾淨的 Dart API,用於解析和產生 GPX 內容,開銷極低。

適用情境

  • 從檔案讀取GPS軌跡
  • 匯出路線資料到 GPX
  • 與地圖和導航應用整合
  • 在設備之間同步GPS資料
  • 處理戶外活動日誌

主要特色

  • 支援航點、路線和軌跡
  • 跨平台相容性
  • 簡單的 XML 解析與產生
  • 輕量且高效
  • 完全符合 GPX 1.1 標準

適合對象

  • 健身和戶外應用
  • GPS資料分析工具
  • 基於地圖的應用程式
  • GPS設備之間的數據交換
  • 開發人員處理地理空間資料

使用建議

  • 需要手動處理檔案 I/O
  • 僅限 GPX 格式
  • 無內建地圖渲染
  • 未針對大型資料集進行優化
  • 依賴外部 XML 解析邏輯

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

gpx

Pub Package Dart Package GitHub Issues GitHub Forks GitHub Stars GitHub License

A Dart library for loading, manipulating, and saving GPS data in GPX format (https://www.topografix.com/gpx.asp), a light-weight XML data format for the interchange of GPS data, including waypoints, routes, and tracks. View the official GPX 1.1 Schema at https://www.topografix.com/GPX/1/1/gpx.xsd.

It also supports exporting Gpx data into:

  • KML (a file format used to display geographic data in an Earth browser such as Google Earth, https://developers.google.com/kml/)
  • CSV (not implemented yet)

Getting Started

In your dart/flutter project add the dependency:

 dependencies:
   ...
   gpx: ^2.5.0
Reading XML

To read XML input use the GpxReader object and function Gpx fromString(String input):

import 'package:gpx/gpx.dart';

void main() {
  // create gpx from xml string
  final xmlGpx = GpxReader().fromString('<?xml version="1.0" encoding="UTF-8"?>'
      '<gpx version="1.1" creator="dart-gpx library">'
      '<wpt lat="-25.7996" lon="-62.8666"><ele>10.0</ele><name>Monte Quemado</name><desc>Argentina</desc></wpt>'
      '</gpx>');

  print(xmlGpx);
  print(xmlGpx.wpts);
}
Writing XML

To write object to XML use the GpxWriter object and function String asString(Gpx gpx, {bool pretty = false}):

import 'package:gpx/gpx.dart';

void main() {
  // create gpx object
  final gpx = Gpx();
  gpx.creator = "dart-gpx library";
  gpx.wpts = [
    Wpt(lat: 36.62, lon: 101.77, ele: 10.0, name: 'Xining', desc: 'China'),
  ];

  // generate xml string
  final gpxString1 = GpxWriter().asString(gpx, pretty: true);
  print(gpxString1);

  // generate xml string with namespaces
  final gpxString2 = GpxWriter().asString(
    gpx,
    namespaces: {
      'trp': 'http://www.garmin.com/xmlschemas/TripExtensions/v1',
    },
    attributes: {
      'xsi:schemaLocation': 'http://www.topografix.com/GPX/1/1 '
          'http://www.topografix.com/GPX/1/1/gpx.xsd',
    },
  );
  print(gpxString2);

  // generate GPX 1.1-compatible xml string with standard namespaces
  final gpxString3 = GpxWriter().asString(
    gpx,
    pretty: true,
    compatibility: GpxCompatibilityMode.gpx11,
  );
  print(gpxString3);
}
Compatibility with strict GPX parsers

Some GPX applications expect the standard GPX 1.1 namespace and schema declarations on the root <gpx> element. Use GpxCompatibilityMode.gpx11 when writing files for applications such as EasyGPS, JPX, Garmin BaseCamp, and other strict GPX parsers:

final xml = GpxWriter().asString(
  gpx,
  pretty: true,
  compatibility: GpxCompatibilityMode.gpx11,
);

This writes a root element with the standard GPX 1.1 declarations:

<gpx xmlns="http://www.topografix.com/GPX/1/1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd"
    version="1.1"
    creator="dart-gpx library">

For Garmin BaseCamp files that also need the Garmin GpxExtensions/v3 namespace, combine the GPX 1.1 compatibility mode with GpxNamespaces.garmin:

final xml = GpxWriter().asString(
  gpx,
  pretty: true,
  compatibility: GpxCompatibilityMode.gpx11,
  namespaces: GpxNamespaces.garmin,
);

Custom namespaces and attributes are applied after the compatibility mode, so they can extend or override the default GPX 1.1 declarations when needed.

Garmin GpxExtensions v3

The package includes typed models for the Garmin GpxExtensions/v3, TrackPointExtension/v1, and WaypointExtension/v1 schemas:

  • GarminWaypointExtension
  • GarminWaypointExtensionV1
  • GarminRouteExtension
  • GarminRoutePointExtension
  • GarminTrackExtension
  • GarminTrackPointExtension
  • GarminTrackPointExtensionV1
final gpx = Gpx()
  ..creator = 'dart-gpx library'
  ..trks = [
    Trk(
      name: 'Morning track',
      typedExtensions: TrkTypedExtensions(
        garmin: GarminTrkExtensions(
          track: GarminTrackExtension(
            displayColor: GarminDisplayColor.darkRed,
          ),
        ),
      ),
      trksegs: [
        Trkseg(
          trkpts: [
            Wpt(
              lat: 51.5,
              lon: -0.1,
              typedExtensions: WptTypedExtensions(
                garmin: GarminWptExtensions(
                  waypointV1: GarminWaypointExtensionV1(
                    samples: 3,
                    expiration: DateTime.utc(2026, 1, 2, 3, 4, 5),
                  ),
                  trackPoint: GarminTrackPointExtension(
                    temperature: 18.5,
                    depth: 4.2,
                  ),
                  trackPointV1: GarminTrackPointExtensionV1(
                    airTemperature: 18.5,
                    heartRate: 142,
                    cadence: 81,
                  ),
                ),
              ),
            ),
          ],
        ),
      ],
    ),
  ];

final xml = GpxWriter().asString(
  gpx,
  pretty: true,
  compatibility: GpxCompatibilityMode.gpx11,
  namespaces: GpxNamespaces.garmin,
);
Export to KML

To export object to KML use the KmlWriter object and function String asString(Gpx gpx, {bool pretty = false}):

import 'package:gpx/gpx.dart';

void main() {
  // create gpx object
  final gpx = Gpx();
  gpx.creator = "dart-gpx library";
  gpx.wpts = [
    Wpt(lat: 36.62, lon: 101.77, ele: 10.0, name: 'Xining', desc: 'China'),
  ];

  // generate xml string
  final kmlString = KmlWriter().asString(gpx, pretty: true);
  print(kmlString);

  // generate xml string with altitude mode - clampToGround
  final kmlStringWithAltitudeMode =
      KmlWriter(altitudeMode: AltitudeMode.clampToGround)
      .asString(gpx, pretty: true);
  print(kmlStringWithAltitudeMode);
}

Limitations

This is just an initial version of the package. There are still some limitations:

  • No support for GPX 1.0.
  • Read/write only from strings.
  • Doesn't validate schema declarations.

Features and bugs

Please file feature requests and bugs at the issue tracker.

License

The Apache 2.0 License, see LICENSE.

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案