google-maps - 如果可以使用 flutter 打开谷歌地图应用程序

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

有什么想法吗?

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?

最佳答案

你可以安装包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);

https://stackoverflow.com/questions/47046637/

相关文章:

flutter - 如何在 flutter 中获得唯一的设备ID?

ios - 在 Flutter 中打开原生 UIViewController

android - 如何在 Flutter 中获取 AppBar 高度

dart - 使用 Flutter/dart 的 NTLM 身份验证

android - 如何使用 Flutter 构建增强现实应用程序?

gridview - Flutter GridView 页脚(用于指示无限滚动的负载)

dart - 为桌面构建 Flutter 应用程序

android - Flutter - 导航到新屏幕,并清除所有以前的屏幕

android - 从原生 Android 主屏幕小部件调用 Flutter (Dart) 代码

android - 是否可以在 flutter 中构建 android 小部件?