Map Launcher 使用教程
map_launcherFlutter plugin for launching maps项目地址:https://gitcode.com/gh_mirrors/ma/map_launcher
项目介绍
Map Launcher 是一个 Flutter 插件,用于发现设备上安装的可用地图应用,并启动它们以显示标记或展示方位。当前支持的地图应用包括 Google Maps、Apple Maps、Google Maps GO、百度地图、高德地图、Waze、Yandex Maps、Yandex Navigator、Citymapper、Maps.me、OsmAnd、OsmAnd+、2GIS 和腾讯地图等。
项目快速启动
添加依赖
在 pubspec.yaml
文件中添加以下依赖:
dependencies:
map_launcher: ^3.4.0
配置 iOS
在 Info.plist
文件中添加 URL schemes:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>baidumap</string>
<string>iosamap</string>
<string>waze</string>
<string>yandexmaps</string>
<string>yandexnavi</string>
<string>citymapper</string>
<string>mapswithme</string>
<string>osmandmaps</string>
<string>dgis</string>
<string>qqmap</string>
<string>here-location</string>
</array>
示例代码
以下是一个简单的示例,展示如何列出已安装的地图应用并启动第一个应用显示标记:
import 'package:flutter/material.dart';
import 'package:map_launcher/map_launcher.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Map Launcher Demo'),
),
body: Center(
child: Builder(
builder: (context) {
return MaterialButton(
onPressed: () => openMapsSheet(context),
child: Text('Show Maps'),
);
},
),
),
),
);
}
void openMapsSheet(BuildContext context) async {
try {
final availableMaps = await MapLauncher.installedMaps;
print(availableMaps);
await availableMaps.first.showMarker(
coords: Coords(37.759392, -122.5107336),
title: "Ocean Beach",
);
} catch (e) {
print(e);
}
}
}
应用案例和最佳实践
显示标记
使用 showMarker
方法在地图上显示标记:
await MapLauncher.showMarker(
mapType: MapType.google,
coords: Coords(37.759392, -122.5107336),
title: "Ocean Beach",
description: "Beautiful beach in San Francisco",
);
显示方向
使用 showDirections
方法显示从当前位置到指定位置的方向:
await MapLauncher.showDirections(
mapType: MapType.google,
destination: Coords(37.759392, -122.5107336),
origin: Coords(37.7749, -122.4194),
);
典型生态项目
Map Launcher 可以与其他 Flutter 插件和库结合使用,例如:
- flutter_svg: 用于显示地图应用的图标。
- geolocator: 用于获取用户当前位置。
- flutter_map: 用于在应用内显示地图。
通过这些插件的结合使用,可以构建出功能丰富的地图应用。
map_launcherFlutter plugin for launching maps项目地址:https://gitcode.com/gh_mirrors/ma/map_launcher