android Flutter -Map中的打开位置

bfrts1fy  于 2023-08-01  发布在  Android
关注(0)|答案(7)|浏览(113)

无论是从长远来看。有没有什么快速/智能的方法来打开Map谷歌/苹果在Flutter和头部的方向?
我正在使用url_launcher进行电话呼叫,我可以使用插件打开打开Map的链接吗?

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

字符串

bihw5rsg

bihw5rsg1#

是的,你可以使用url_launcher插件。以下代码将在手机上安装应用程序时打开GoogleMap(否则将打开浏览器):

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

字符串

jjhzyzn0

jjhzyzn02#

我为此创建了一个插件Map Launcher
要在设备上查找已安装的Map,您可以用途:

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",
);


或者只是检查Map是否可用,如果是,就启动它

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

myzjeezk

myzjeezk3#

该解决方案将在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,“address”);

rt4zxlrg

rt4zxlrg4#

如果你想使用address代替lat,long,你可以使用下面的代码。
参考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>

wwodge7n

wwodge7n5#

用于这个原生方法

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

字符串
而在android软件包中

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()
        }
    }
}


}

c90pui9n

c90pui9n6#

2022解决方案

void _launchMapsUrl() async {
if (_locationData == null) return;
final url =
    'https://www.google.com/maps/search/?api=1&query=${_locationData!.latitude},${_locationData!.longitude}';

Map<String, dynamic>? params = {
  'api': '1',
  'query': '${_locationData!.latitude},${_locationData!.longitude}'
};
final uri = Uri.https(
  'www.google.com',
  '/maps/search/',
  params,
);
if (await canLaunchUrl(uri)) {
  await launchUrl(uri);
} else {
  _showErrorDialog(
      errorText:
          'Could not launch.\nCheck please if you have Google Maps Application on your device.');
  throw 'Could not launch $url';
}

字符串
}

6tqwzwtp

6tqwzwtp7#

import 'package:url_launcher/url_launcher_string.dart';

void openMap(lat,long) async {
    try {
      await launchUrlString('https://www.google.com/maps/search/?api=1&query=$lat,$long', 
                             mode: LaunchMode.externalApplication);
    } catch (e) {
      debugPrint(  "🚀 catched error~ $e:");
    }
  }

字符串

相关问题