flutter url_launcher打开外部软件调用高德地图导航

发布于 1周前 作者 phonegap100 最后一次编辑是 5天前 来自 分享

对应视频教程教程访问https://www.itying.com/goods-1176.html

flutter调用高德地图导航实现导航可以使用url_launcher来打开外部软件

地址:https://pub.dev/packages/url_launcher

一 、flutter使用 url_launcher打开外部软件调用高德地图导航 Android配置

修改:android\app\src\main\AndroidManifest.xml

  <!-- Provide required visibility configuration for API level 30 and above -->
    <queries>
    <!-- If your app checks for SMS support -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="sms" />
    </intent>
    <!-- If your app checks for call support -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="tel" />
    </intent>
    </queries>
    <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

二、flutter使用 url_launcher打开外部软件调用高德地图导航 Ios配置

配置拨打电话、发送邮件、打开外部应用、高德地图导航Info.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
     <string>iosamap</string>
     <string>baidumap</string>
     <string>sms</string>
     <string>tel</string>
     <string>weixin</string>
     <string>alipays</string>
    </array>

高德官网导航地址:

https://lbs.amap.com/api/amap-mobile/guide/android/navigation

坐标吸取器

https://lbs.amap.com/tools/picker

三、 完整代码

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class UrlLauncherPage extends StatefulWidget {
  const UrlLauncherPage({super.key});

  @override
  State<UrlLauncherPage> createState() => _UrlLauncherPageState();
}

class _UrlLauncherPageState extends State<UrlLauncherPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('UrlLauncherPage'),
        ),
        body: Center(
            child: Padding(
          padding: const EdgeInsets.all(20),
          child: ListView(children: [
            ElevatedButton(
              child: const Text('打开外部浏览器'),
              onPressed: () async {
                final Uri url = Uri.parse('https://www.itying.com');
                if (await canLaunchUrl(url)) {
                  await launchUrl(url);
                } else {
                  throw 'Could not launch $url';
                }
              },
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              child: const Text('拨打电话'),
              onPressed: () async {
                final Uri tel = Uri.parse('tel:10086');
                if (await canLaunchUrl(tel)) {
                  await launchUrl(tel);
                } else {
                  throw 'Could not launch $tel';
                }
              },
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              child: const Text('发送短信'),
              onPressed: () async {
                final Uri tel = Uri.parse('sms:10086');
                //  var tel = 'sms:10086';
                if (await canLaunchUrl(tel)) {
                  await launchUrl(tel);
                } else {
                  throw 'Could not launch $tel';
                }
              },
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              child: const Text('打开外部应用-支付宝'),
              onPressed: () async {
                /*
                    weixin://
                    alipays://
                  */
                final Uri alipays = Uri.parse('alipays://');
                if (await canLaunchUrl(alipays)) {
                  await launchUrl(alipays);
                } else {
                  throw 'Could not launch $alipays';
                }
              },
            ),
            const SizedBox(height: 10),
            ElevatedButton(
              child: const Text('打开外部应用-微信'),
              onPressed: () async {
                /*
                    weixin://
                    alipays://
                  */
                final Uri weixin = Uri.parse('weixin://');
                if (await canLaunchUrl(weixin)) {
                  await launchUrl(weixin);
                } else {
                  throw 'Could not launch $weixin';
                }
              },

              //
            ),
            const SizedBox(height: 10),
            ElevatedButton(
                child: const Text('打开外部应用-高德地图'),
                onPressed: () async {
                  String title = "北京大学";
                  String latitude = "39.992806";
                  String longitude = "116.310905";

                  Uri uri = Uri.parse(
                      '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2&poiname=${title}');
                  // if (Platform.isIOS) url = Uri.encodeFull(url);

                  // if (Platform.isIOS) url = Uri.encodeFull(url);
                  // url=Uri.parse(url);
                  try {
                    if (await canLaunchUrl(uri)) {
                      await launchUrl(uri);
                    } else {
                      print('无法调起高德地图');
                    }
                  } catch (e) {
                    print('无法调起高德地图');
                  }
                }),
          ]),
        )));
  }
}

回到顶部