如果有颤动,请打开Goog​​le地图应用

Fre*_*ook 13 flutter

我正在尝试检测是否在iOS上安装了Google地图应用,如果是,请启动它,如果没有,则启动Apple Maps.这是我到目前为止所拥有的,但在安装了谷歌地图的手机上,它没有检测到并正确启动.

有任何想法吗?

import 'package:url_launcher/url_launcher.dart';

_launchMaps() async {
  String googleUrl =
    'comgooglemaps://?center=${trip.origLocationObj.lat},${trip.origLocationObj.lon}';
  String appleUrl =
    'https://maps.apple.com/?sll=${trip.origLocationObj.lat},${trip.origLocationObj.lon}';
  if (await canLaunch("comgooglemaps://")) {
    print('launching com googleUrl');
    await launch(googleUrl);
  } else if (await canLaunch(appleUrl)) {
    print('launching apple url');
    await launch(appleUrl);
  } else {
    throw 'Could not launch url';
  }
}
Run Code Online (Sandbox Code Playgroud)

我从这里取出网址方案:当我在我的应用程序中按下按钮时,我怎么能打开谷歌地图?

Bha*_*pal 20

static Future<void> openMap(BuildContext context, double lat, double lng) async {
    String url = '';
    String urlAppleMaps = '';
    if (Platform.isAndroid) {
      url = 'https://www.google.com/maps/search/?api=1&query=$lat,$lng';
      if (await canLaunchUrl(Uri.parse(url))) {
        await launchUrl(Uri.parse(url));
      } else {
        throw 'Could not launch $url';
      }
    } else {
      urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';
      url = 'comgooglemaps://?saddr=&daddr=$lat,$lng&directionsmode=driving';
      if (await canLaunchUrl(Uri.parse(url))) {
        await launchUrl(Uri.parse(url));
      } else if (await canLaunchUrl(Uri.parse(urlAppleMaps))) {
        await launchUrl(Uri.parse(urlAppleMaps));
      } else {
        throw 'Could not launch $url';
      }
    }
}
Run Code Online (Sandbox Code Playgroud)


Fre*_*ook 13

我发现了我的问题:这需要在plist文件中.上面问题中的代码很好.(问题中引用的SO答案仅提到了"comgooglemaps"字符串.)

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>googlechromes</string>
    <string>comgooglemaps</string>
</array>
Run Code Online (Sandbox Code Playgroud)

文档:https://developers.google.com/maps/documentation/ios-sdk/start#step_7_declare_the_url_schemes_used_by_the_api


小智 12

使用网址启动器

在 yaml 文件中: url_launcher: ^5.0.2 最后

然后您可以使用此方法打开以提供的纬度和经度为中心的谷歌地图

more info to maps intent from docs [here][2]

launchMap({String lat = "47.6", String long = "-122.3"}) async{
  var mapSchema = 'geo:$lat,$long';
  if (await canLaunch(mapSchema)) {
    await launch(mapSchema);
  } else {
    throw 'Could not launch $mapSchema';
  }
}
Run Code Online (Sandbox Code Playgroud)


Roc*_*nat 10

如果您没有实际的latlong,您可以简单地将地址传递给 Google 地图。

void launchMap(String address) async {
  String query = Uri.encodeComponent(address);
  String googleUrl = "https://www.google.com/maps/search/?api=1&query=$query";

  if (await canLaunch(googleUrl)) {
    await launch(googleUrl);
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,地址中的信息越多,搜索就越准确。与在实际的 Google 地图页面或应用程序上查找内容完全相同。

顺便说一句,在将地址添加到 URL 之前,您需要对地址进行 url 编码,以支持空格等特殊字符。它仅适用于 iOS,但是嘿,我们希望为所有环境进行开发。


Cla*_*Luz 9

您可以安装packege url_launcher并使用下面的代码:

import 'package:url_launcher/url_launcher.dart';

class MapUtils {

  MapUtils._();

  static Future<void> openMap(double latitude, double longitude) async {
    String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
    if (await canLaunch(googleUrl)) {
      await launch(googleUrl);
    } else {
      throw 'Could not open the map.';
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在应用中打开Goog​​le地图,只需调用此方法即可:

MapUtils.openMap(-3.823216,-38.481700);
Run Code Online (Sandbox Code Playgroud)

  • 这将拉动 iOS 上的浏览​​器而不是应用程序。 (2认同)
  • 抱歉,我将在iOS中尝试,之后我回到这里并更新帖子以解决此问题,请按您的评论。 (2认同)

flu*_*oob 8

作为 Roc Boronat帖子的后续内容,以下代码可用于启动特定于平台的地图应用程序。

Future<void> launchMapUrl(String address) async {
    String encodedAddress = Uri.encodeComponent(address);
    String googleMapUrl = "https://www.google.com/maps/search/?api=1&query=$encodedAddress";
    String appleMapUrl = "http://maps.apple.com/?q=$encodedAddress";
    if (Platform.isAndroid) {
      try {
        if (await canLaunch(googleMapUrl)) {
          await launch(googleMapUrl);
        }
      } catch (error) {
        throw("Cannot launch Google map");
      }
    }
    if (Platform.isIOS) {
      try {
        if (await canLaunch(appleMapUrl)) {
          await launch(appleMapUrl);
        }
      } catch (error) {
        throw("Cannot launch Apple map");
      }
    }
Run Code Online (Sandbox Code Playgroud)

有关 Apple 地图 URL 中的查询参数的更多信息,请访问此链接

编辑(2022 年 8 月 7 日):此代码适用于插件版本 6.0.20 url_launcher。在此版本之后我无法让它工作,因为我在尝试使用6.0.20 和 6.0.20 以上版本的插件ERR_UNKNOWN_URL_SCHEME启动 Google 地图时遇到错误。它仅适用于已弃用的方法 (和)。如果有人想尝试此代码片段,请注意。canLaunchUrllaunchUrlcanLaunchlaunch


Cod*_*ner 7

如果您想使用方向导航,您只需创建一个带有源坐标和目的地坐标以及其他坐标的 url 以添加为停靠点。

步骤: 1. 安装url_launcher插件

  1. 编写如下代码。
_launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }




const url ='https://www.google.com/maps/dir/?api=1&origin=43.7967876,-79.5331616&destination=43.5184049,-79.8473993&waypoints=43.1941283,-79.59179|43.7991083,-79.5339667|43.8387033,-79.3453417|43.836424,-79.3024487&travelmode=driving&dir_action=navigate';
_launchURL(url);
Run Code Online (Sandbox Code Playgroud)


小智 7

如果您想在设备上可用的外部应用程序中打开地图,请添加启动 url 属性,即LaunchMode。LaunchMode 有几个选项,您可以根据应用程序的需求进行选择。启动模式选项是,

  • LaunchMode.platformDefault - 这是默认模式,将启动 URL 的决定留给平台。
  • LaunchMode.inAppWebView - 在应用程序内 Web 视图中加载 URL。
  • LaunchMode.externalApplication - 将 URL 传递到操作系统以由另一个应用程序处理。
  • LaunchMode.externalNonBrowserApplication - 将 URL 传递到操作系统以由另一个非浏览器应用程序处理
  1. url_launcher添加到您的 pubspec.yaml 文件中

    url_launcher: LATEST_VERSION
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用下面给出的函数,

    Future<void> openMap(double latitude, double longitude, {LaunchMode linkLaunchMode = LaunchMode.externalApplication}) async {
        String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
         if (await canLaunchUrl(Uri.parse(googleUrl))) {
               await launchUrl(Uri.parse(googleUrl), mode: linkLaunchMode);
         } else {
               throw 'Could not open the map.';
         }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以在上面的函数中看到,该函数具有作为外部应用程序的默认启动模式,因此每当用户单击链接时,它将在外部谷歌地图应用程序中打开。
  • 您还可以根据您的功能需求自定义此功能。


Muh*_*fay 6

import 'package:url_launcher/url_launcher.dart';

static void launchMapsUrl(
      sourceLatitude,
      sourceLongitude,
      destinationLatitude,
      destinationLongitude) async {
    String mapOptions = [
      'saddr=$sourceLatitude,$sourceLongitude',
      'daddr=$destinationLatitude,$destinationLongitude',
      'dir_action=navigate'
    ].join('&');

    final url = 'https://www.google.com/maps?$mapOptions';
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }  }
Run Code Online (Sandbox Code Playgroud)

这里可以直接使用这个函数,传入需要的参数,也可以导入这个包https://pub.dev/packages/url_launcher/


小智 6

这样做

完整代码如下

static void navigateTo(double lat, double lng) async {
   var uri = Uri.parse("google.navigation:q=$lat,$lng&mode=d");
   if (await canLaunch(uri.toString())) {
      await launch(uri.toString());
   } else {
      throw 'Could not launch ${uri.toString()}';
   }
}
Run Code Online (Sandbox Code Playgroud)

1)在 pubspec.yaml 中

dependencies:
  flutter:
    sdk: flutter
    ...
    url_launcher: ^5.7.8
Run Code Online (Sandbox Code Playgroud)

2)在任何你想使用的地方导入

import 'package:url_launcher/url_launcher.dart';
Run Code Online (Sandbox Code Playgroud)

3)最后你打电话

onPressed: () {
   navigateTo(location.lat, location.lng);
},
Run Code Online (Sandbox Code Playgroud)