Map Launcher 使用教程

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

伍希望

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值