我正在使用 Flutter 的maps_launcher包https://pub.dev/packages/maps_launcher,它在打开iOS版苹果地图时通过直接进入应用程序而不是简单地显示Web视图页面来完成它的工作。我的函数 openMap() 也可以在 Android 中使用 Google 地图,但我似乎无法直接进入我的真实 iOS 设备上的 Google 地图应用程序,该设备已安装并运行 Google 地图。它仅打开 Google 地图的网络视图。有什么想法如何让它自动转到 Google 地图应用程序吗?
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:maps_launcher/maps_launcher.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io' show Platform;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MediaQuery(
data: MediaQueryData(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(),
),
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
final myActionSheet = CupertinoActionSheet(
actions: [
CupertinoActionSheetAction(
child: Text('Open in Maps'),
onPressed: (){
MapsLauncher.launchCoordinates(
37.3608503, -122.0581159, '1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA');
},
),
CupertinoActionSheetAction(
child: Text('Open in Google Maps'),
onPressed: (){
openMap(37.4220041, -122.0862462);
},
),
],
);
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.';
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'Maps Launcher Demo',
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
FloatingActionButton(
onPressed: Platform.isIOS ?
() {
showCupertinoModalPopup(context: context, builder: (context) => myActionSheet);
} :
(){
openMap(37.4220041, -122.0862462);
},
child: Icon(Icons.directions),
),
],
),
),
);
}
}
我使用 url_launcher 包和以下 URL 方案:
comgooglemaps:// 和 comgooglemaps-x-callback:// - 这些方案允许您启动 iOS 版 Google 地图应用程序并执行以下几个操作之一:
在指定位置和缩放级别显示地图。
搜索位置或地点,并将其显示在地图上。
请求从一个地点到另一地点的路线。可以返回四种交通方式的路线:驾车、步行、骑自行车和公共交通。
向您的应用程序添加导航。
通过 iOS 8,在应用程序完成时使用 comgooglemaps-x-callback:// 发出回调。回调通常用于将用户返回到最初打开 iOS 版 Google 地图的应用。请注意,在 iOS 9 上,系统会自动在状态栏左上角提供“返回”链接。
示例:
comgooglemaps://?center=$latitude,$longitude&zoom=14
更多信息:ios-urlscheme
无需
comgooglemaps://
或 comgooglemaps-x-callback://
方案即可完成。 Google 推荐通用链接。
使用
url_launcher
,您很可能需要将 mode
定义为 externalApplication
。 不要忘记模式,否则它可能会一直在应用内 Safari 中打开。
launchUrl(
// it can be any Google Maps link, no limitations like in the case of comgooglemaps.
// As long as it works on the web, it will work here.
Uri.parse("https://maps.app.goo.gl/osFFuRVctqCicwzz6"),
mode: LaunchMode.externalApplication,
)