繁体   English   中英

如何在 Flutter 中打开 ios 中的谷歌地图应用程序

[英]How to open the Google Maps App in ios in Flutter

I'm using the maps_launcher package for Flutter https://pub.dev/packages/maps_launcher , and it's doing it's job when opening apple maps for ios by going directly into the app and not simply displaying a web view page. My function openMap() is also doing the trick for Google Maps in Android, but I can't seem to go directly into the Google Maps app on my real iOS device, which has Google Maps installed and working. 它仅打开 Google 地图的 web 视图。 任何想法如何使其自动 go 到谷歌地图应用程序?

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 package 和以下 URL 方案:

comgooglemaps://comgooglemaps-x-callback:// - 这些方案允许您启动 iOS 的 Google 地图应用程序并执行以下操作之一:

  • 在指定位置和缩放级别显示 map。

  • 搜索位置或地点,并将它们显示在 map 上。

  • 请求从一个位置到另一个位置的路线。 可以返回四种交通方式的路线:驾车、步行、骑自行车和公共交通。

  • 为您的应用添加导航。

  • 通过 iOS 8,在应用程序完成时使用 comgooglemaps-x-callback:// 发出回调。 回调通常用于将用户返回到最初为 iOS 打开 Google 地图的应用程序。 请注意,在 iOS 9 上,系统会在状态栏的左下角自动提供“返回”链接。

示例: comgooglemaps://?center=$latitude,$longitude&zoom=14

更多信息: ios-urlscheme

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM