【问题标题】:Flutter - Open Location in MapsFlutter - 在地图中打开位置
【发布时间】:2018-09-30 12:16:23
【问题描述】:

给定纬度和经度。 有没有什么快速/智能的方法可以在 Flutter 中打开地图 google/apple 并前往路线?

我正在使用 url_launcher 进行电话呼叫,我可以使用插件打开打开地图的链接吗?

_launchURL() async {
  const url = 'https://www.google.com/maps/place/Al+qi,+ADoqi,+Giza+Governorate/@33.0523046,38.2009323,17z/data=!3m1!4b1!4m5!3m4!1s0x1458413a996ec217:0x2411f6b62d93ccc!8m2!3d30.05237!4d31.2031598';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

【问题讨论】:

标签: android ios flutter


【解决方案1】:

是的,您可以使用 url_launcher 插件来做到这一点。以下代码会在手机安装应用时打开谷歌地图(否则会打开浏览器):

void _launchMapsUrl(double lat, double lon) async {
  final url = 'https://www.google.com/maps/search/?api=1&query=$lat,$lon';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

【讨论】:

  • 也可以只使用地址,例如:https://www.google.com/maps/search/${Uri.encodeFull(address)}
  • 在 iOS 中,这只会打开浏览器
【解决方案2】:

我为此创建了一个插件Map Launcher

要在您可以使用的设备上查找已安装的地图:

import 'package:map_launcher/map_launcher.dart';

final availableMaps = await MapLauncher.installedMaps;

然后调用 showMarker 启动

await availableMaps.first.showMarker(
  coords: Coords(31.233568, 121.505504),
  title: "Shanghai Tower",
  description: "Asia's tallest building",
);

或者只是检查地图是否可用,如果可用则启动它

if (await MapLauncher.isMapAvailable(MapType.google)) {
  await MapLauncher.launchMap(
    mapType: MapType.google,
    coords: coords,
    title: title,
    description: description,
  );
}

【讨论】:

  • 我收到了这个错误:MissingPluginException(No implementation found for method getInstalledMaps on channel map_launcher) 我该如何解决这个问题?
  • @emreuguryalcintr,只需终止运行'main.dart'并再次构建应用程序
  • 为合适的设备使用合适的地图:mapType: Theme.of(context).platform == TargetPlatform.iOS ? MapLauncher.MapType.apple : MapLauncher.MapType.google
  • 这段代码运行良好。但是,为什么“标题”和“描述”没有显示在 android 的谷歌地图中?在 iOS 中没问题。
  • 这个插件很棒!非常感谢!
【解决方案3】:

此解决方案适用于 Android 和 iOS 平台。

import 'dart:io' show Platform;

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

class MapsLauncher {

 static String createQueryUrl(String query) {
  var uri;

  if (kIsWeb) {
   uri = Uri.https(
      'www.google.com', '/maps/search/', {'api': '1', 'query': query});
  } else if (Platform.isAndroid) {
   uri = Uri(scheme: 'geo', host: '0,0', queryParameters: {'q': query});
  } else if (Platform.isIOS) {
   uri = Uri.https('maps.apple.com', '/', {'q': query});
  } else {
   uri = Uri.https(
      'www.google.com', '/maps/search/', {'api': '1', 'query': query});
 }

 return uri.toString();
 }

static String createCoordinatesUrl(double latitude, double longitude,
  [String? label]) {
 var uri;

 if (kIsWeb) {
   uri = Uri.https('www.google.com', '/maps/search/',
      {'api': '1', 'query': '$latitude,$longitude'});
 } else if (Platform.isAndroid) {
   var query = '$latitude,$longitude';

   if (label != null) query += '($label)';

   uri = Uri(scheme: 'geo', host: '0,0', queryParameters: {'q': query});
 } else if (Platform.isIOS) {
   var params = {'ll': '$latitude,$longitude'};

   if (label != null) params['q'] = label;

   uri = Uri.https('maps.apple.com', '/', params);
 } else {
   uri = Uri.https('www.google.com', '/maps/search/',
      {'api': '1', 'query': '$latitude,$longitude'});
 }

 return uri.toString();
 }

 static Future<bool> launchQuery(String query) {
  return launch(createQueryUrl(query));
 }

 static Future<bool> launchCoordinates(double latitude, double longitude,
  [String? label]) {
 return launch(createCoordinatesUrl(latitude, longitude, label));
 }
}

这样称呼它: MapsLauncher.launchCoordinates( 37.4220041, -122.0862462, "地址");

【讨论】:

    【解决方案4】:

    如果你想使用地址而不是经纬度,你可以使用以下。

    引用https://developers.google.com/maps/documentation/urls/ios-urlschemedaddr 设置路线搜索的终点

    final url = 'comgooglemaps://?daddr=${Uri.encodeFull(address)}&directionsmode=driving';
        if (await canLaunch(url)) {
          await launch(url);
        } else {
          throw 'Could not launch $url';
        }
    

    别忘了添加到 Info.plist

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

    【讨论】:

      【解决方案5】:

      我用这个原生方法

      try {
        await platform.invokeMethod('showLocation', [event.location[0], event.location[1], event.title]);
      } on Exception catch (ex) {
        print('${ex.toString()}');
        ex.toString();
      }
      

      在安卓包中

      class MainActivity : FlutterActivity() {
      
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          GeneratedPluginRegistrant.registerWith(this)
          MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
              if (call.method == "showLocation") {
                  val args = (call.arguments) as List<Any>
                  startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("geo:${args[0]}, ${args[1]}?z=23&q=${args[0]},${args[1]}(${args[2]})")))
                  result.success(null)
              } else {
                  result.notImplemented()
              }
          }
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-21
        • 2023-04-05
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        • 2021-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多