Flutter地理位置获取插件transport_gd_location的使用
Flutter地理位置获取插件transport_gd_location
的使用
描述
transport_gd_location
是一个基于 Flutter 封装的网络货运信息交互系统位置信息上传插件,支持 iOS 和 Android 平台。该插件基于高德地图实现,旨在满足《交通运输部办公厅关于进一步做好网络平台道路货物运输信息化监测工作的通知》的要求。
使用步骤
1. 添加依赖
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
transport_gd_location: ^版本号
然后运行以下命令安装依赖:
flutter pub get
2. 初始化插件
首先,需要调用 initSdk
方法初始化插件,并传入高德地图的 App Key。
Future<void> initSdk() async {
bool success;
try {
success = await _transportGdLocationPlugin.initSdk(gdAppKey: "您的高德地图 App Key") ?? false;
} on PlatformException {
success = false;
}
print("初始化 - 结果 = $success");
}
3. 打开服务
通过 openService
方法打开位置信息上传服务。需要配置 ServiceConfig
参数,包括应用 ID、安全密钥、企业发送方代码和环境类型。
Future<void> openService() async {
ResultModel? resultModel;
try {
ServiceConfig serviceConfig = ServiceConfig(
appId: Platform.isAndroid
? "com.hswl.transport_gd_location.transport_gd_location_example"
: "com.hswl.transportgdlocation.transportGdLocationExample",
appSecurity: Platform.isAndroid
? "cd5a822984cd48c3a9a92c09e0868cb6b137e6bf336d456fa1b7696308449f05"
: "fd6a81f2a41b4ea891269067e7eb4b68299ca62797ab4c348274f13d84d580bc",
enterpriseSenderCode: "10002",
environment: "debug",
);
resultModel = await _transportGdLocationPlugin.openService(serviceConfig: serviceConfig);
} catch (e) {
resultModel = null;
}
print("授权 - 结果 = ${resultModel?.toJson()}");
}
4. 示例代码
以下是一个完整的示例代码,展示了如何初始化插件并打开服务:
import 'dart:io';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:transport_gd_location/transport_gd_location.dart';
import 'package:transport_gd_location/model/result_model.dart';
import 'package:transport_gd_location/model/service_config.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
final _transportGdLocationPlugin = TransportGdLocation();
[@override](/user/override)
void initState() {
super.initState();
initSdk();
}
// 初始化插件
Future<void> initSdk() async {
bool success;
try {
success = await _transportGdLocationPlugin.initSdk(gdAppKey: "您的高德地图 App Key") ?? false;
} on PlatformException {
success = false;
}
print("初始化 - 结果 = $success");
}
// 打开服务
Future<void> openService() async {
ResultModel? resultModel;
try {
ServiceConfig serviceConfig = ServiceConfig(
appId: Platform.isAndroid
? "com.hswl.transport_gd_location.transport_gd_location_example"
: "com.hswl.transportgdlocation.transportGdLocationExample",
appSecurity: Platform.isAndroid
? "cd5a822984cd48c3a9a92c09e0868cb6b137e6bf336d456fa1b7696308449f05"
: "fd6a81f2a41b4ea891269067e7eb4b68299ca62797ab4c348274f13d84d580bc",
enterpriseSenderCode: "10002",
environment: "debug",
);
resultModel = await _transportGdLocationPlugin.openService(serviceConfig: serviceConfig);
} catch (e) {
resultModel = null;
}
print("授权 - 结果 = ${resultModel?.toJson()}");
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Text('Running on: $_platformVersion\n'),
),
),
);
}
}
1 回复