【问题标题】:Open Google Maps app if available with flutter如果可以使用颤振打开谷歌地图应用程序
【发布时间】:2018-04-13 06:50:57
【问题描述】:

我正在尝试检测 iOS 上是否安装了 Google Maps 应用,如果是,则启动它,如果没有,启动 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';
  }
}

我从这里提取了 url 方案:How would I be able to open google maps when I press a button in my app?

【问题讨论】:

    标签: google-maps flutter


    【解决方案1】:

    您可以安装包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.';
        }
      }
    }
    

    现在你可以在你的应用中打开谷歌地图,只需调用这个方法:

    MapUtils.openMap(-3.823216,-38.481700);
    

    【讨论】:

    • 这将拉取iOS上的浏览器而不是应用程序。
    • 对不起,我会在 iOS 中尝试,然后我会回到这里并更新帖子以解决这个问题,感谢您的评论。
    • 哪个包包含'MapUtils'?
    • @sumitmehra 这不是一个包,而是一个你自己写的类:)
    【解决方案2】:

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

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>googlechromes</string>
        <string>comgooglemaps</string>
    </array>
    

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

    【讨论】:

    • 我收到此错误。请帮忙。 MissingPluginException(在通道 plugins.flutter.io/url_launcher 上找不到方法 canLaunch 的实现)
    • @RachitRawat 重建。 FrederickCook 你需要为 Android 配置什么吗?
    • @RachitRawat,也有同样的问题。试试flutter clean
    • 如何自动导航?
    【解决方案3】:

    使用网址启动器

    在 yaml 文件中:url_launcher:^5.0.2 last

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

    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';
      }
    }
    

    【讨论】:

    • 不工作,抛出错误
    【解决方案4】:

    如果您想使用路线导航,您只需创建一个带有源坐标和目标坐标以及其他坐标的 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);
    

    【讨论】:

      【解决方案5】:

      如果您没有实际的 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);
        }
      }
      

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

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

      【讨论】:

        【解决方案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()}';
           }
        }
        

        1) 在 pubspec.yaml 中

        dependencies:
          flutter:
            sdk: flutter
            ...
            url_launcher: ^5.7.8
        

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

        import 'package:url_launcher/url_launcher.dart';
        

        3) 最终调用

        onPressed: () {
           navigateTo(location.lat, location.lng);
        },
        

        【讨论】:

        • 刚刚在Andoid上检查过,效果很好!谢谢
        【解决方案7】:
        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';
            }  }
        

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

        【讨论】:

        • 这是我真正想要的,但由于某种原因,即使在 android manifest 文件中设置了查询,我也无法启动 url。
        • 我把 flutter clean 跑了一遍,然后又试了一次,结果成功了。
        【解决方案8】:
        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';
        } else {
          urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';
          url = 'comgooglemaps://?saddr=&daddr=$lat,$lng&directionsmode=driving';
          if (await canLaunch(url)) {
            await launch(url);
          } else {
            throw 'Could not launch $url';
          }
        }
        
        if (await canLaunch(url)) {
          await launch(url);
        } else if (await canLaunch(urlAppleMaps)) {
          await launch(urlAppleMaps);
        } else {
          throw 'Could not launch $url';
        }}
        

        【讨论】:

          【解决方案9】:

          试试这个功能

          使用插件: 意图:^1.4.0

          static void navigateTo(double lat, double lng) async {
              var uri = Uri.parse("google.navigation:q=$lat,$lng&mode=c");
              android_intent.Intent()
                ..setAction(android_action.Action.ACTION_VIEW)
                ..setData(uri)
                ..setPackage("com.google.android.apps.maps")
                ..startActivity().catchError((e) => print(e));
            }
          

          【讨论】:

            猜你喜欢
            • 2020-12-13
            • 2020-07-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-15
            • 1970-01-01
            相关资源
            最近更新 更多