Flutter应用内导航启动插件navigation_launcher的使用
Flutter应用内导航启动插件navigation_launcher的使用
navigation_launcher
navigation_launcher
是一个用于在 Flutter 应用中启动导航功能的插件。它允许用户通过点击按钮或列表项来调用已安装的地图应用进行导航。
使用步骤
1. 添加依赖
在 pubspec.yaml
文件中添加 navigation_launcher
依赖:
dependencies:
navigation_launcher: ^1.0.0
然后运行以下命令以更新依赖:
flutter pub get
2. 初始化插件
在项目中初始化插件并获取已安装的地图应用列表。
示例代码:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:navigation_launcher/navigation_launcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<MapApp>? _maps; // 存储已安装的地图应用
final _navigationLauncherPlugin = NavigationLauncher(); // 导航插件实例
@override
void initState() {
super.initState();
initPlatformState(); // 初始化平台状态
}
// 异步方法:获取已安装的地图应用
Future<void> initPlatformState() async {
List<MapApp>? maps;
// 调用插件方法获取已安装的地图应用
try {
maps = await _navigationLauncherPlugin.getInstalledMaps();
} on PlatformException {
maps = []; // 如果发生异常,返回空数组
}
// 如果当前组件已经卸载,则不更新状态
if (!mounted) return;
setState(() {
_maps = maps; // 更新地图应用列表
});
}
@override
Widget build(BuildContext context) {
final maps = _maps;
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('导航插件示例'),
),
body: maps == null
? const Center(child: CircularProgressIndicator()) // 加载中
: Center(
child: ListView.builder(
itemBuilder: (context, index) => ListTile(
title: Text(maps[index].name), // 显示地图应用名称
onTap: () {
// 启动导航
_navigationLauncherPlugin.launchNavigation(
maps[index], // 选择的地图应用
const LatLng(39.4, 115.7), // 目标坐标
name: '北京', // 目标地点名称
);
},
),
itemCount: maps.length, // 列表项数量
),
),
),
);
}
}
功能说明
1. 获取已安装的地图应用
通过调用 _navigationLauncherPlugin.getInstalledMaps()
方法,可以获取设备上已安装的地图应用列表。这些应用通常包括 Google Maps、高德地图等。
List<MapApp>? maps = await _navigationLauncherPlugin.getInstalledMaps();
2. 启动导航
通过调用 _navigationLauncherPlugin.launchNavigation()
方法,可以启动选定的地图应用并设置导航目标。
_navigationLauncherPlugin.launchNavigation(
maps[index], // 地图应用对象
const LatLng(39.4, 115.7), // 目标坐标
name: '北京', // 目标地点名称
);
更多关于Flutter应用内导航启动插件navigation_launcher的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter应用内导航启动插件navigation_launcher的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
navigation_launcher
是一个用于在 Flutter 应用中启动导航的插件。它允许你通过调用设备上已安装的导航应用(如 Google Maps、Apple Maps 等)来启动导航到指定目的地。
安装
首先,你需要在 pubspec.yaml
文件中添加 navigation_launcher
插件的依赖:
dependencies:
flutter:
sdk: flutter
navigation_launcher: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装依赖。
基本用法
-
导入包
在你的 Dart 文件中导入
navigation_launcher
包:import 'package:navigation_launcher/navigation_launcher.dart';
-
启动导航
使用
NavigationLauncher.launchNavigation
方法来启动导航。你需要提供目的地的经纬度坐标。void launchNavigation() async { double latitude = 37.7749; // 目的地的纬度 double longitude = -122.4194; // 目的地的经度 String destinationName = "San Francisco"; // 目的地的名称(可选) try { await NavigationLauncher.launchNavigation( latitude: latitude, longitude: longitude, destinationName: destinationName, ); } catch (e) { print("无法启动导航: $e"); } }
-
在 UI 中调用
你可以在按钮的
onPressed
事件中调用launchNavigation
方法:ElevatedButton( onPressed: launchNavigation, child: Text("启动导航"), );
参数说明
latitude
(必填): 目的地的纬度。longitude
(必填): 目的地的经度。destinationName
(可选): 目的地的名称,用于在导航应用中显示。startLatitude
(可选): 起点的纬度。startLongitude
(可选): 起点的经度。startName
(可选): 起点的名称。
示例
以下是一个完整的示例,展示如何在 Flutter 应用中使用 navigation_launcher
插件:
import 'package:flutter/material.dart';
import 'package:navigation_launcher/navigation_launcher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("导航示例"),
),
body: Center(
child: ElevatedButton(
onPressed: launchNavigation,
child: Text("启动导航"),
),
),
),
);
}
void launchNavigation() async {
double latitude = 37.7749; // 目的地的纬度
double longitude = -122.4194; // 目的地的经度
String destinationName = "San Francisco"; // 目的地的名称(可选)
try {
await NavigationLauncher.launchNavigation(
latitude: latitude,
longitude: longitude,
destinationName: destinationName,
);
} catch (e) {
print("无法启动导航: $e");
}
}
}
注意事项
-
确保设备上已安装至少一个导航应用(如 Google Maps、Apple Maps 等)。
-
在 Android 上,你可能需要在
AndroidManifest.xml
中添加以下权限:<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
-
在 iOS 上,你可能需要在
Info.plist
中添加以下键值对:<key>NSLocationWhenInUseUsageDescription</key> <string>我们需要您的位置信息来启动导航。</string>