简体   繁体   English

如何使用 Flutter/Dart 打开 Apple Maps?

[英]How to open Apple Maps using Flutter/Dart?

Our Flutter App shows a number of locations using Google Maps, if available, or else using the local browser.我们的 Flutter 应用程序使用 Google 地图(如果可用)或使用本地浏览器显示多个位置。

Although we had already previously uploaded a binary code for iOS which was accepted by Apple and successfully published in the App Store, now that we have added some more locations and thus attempted to publish a new version, Apple has rejected our binary , stating that it is mandatory to use "Apple Maps" instead of anything that starts with a "G" , like Google ...虽然我们之前已经为 iOS 上传了一个被苹果接受并成功发布在 App Store 的二进制代码,但现在我们添加了更多的位置并因此尝试发布一个新版本,苹果拒绝了我们的二进制文件,指出它必须使用“Apple Maps”而不是任何以“G”开头的东西,比如谷歌......

The rejection message reads as follows:拒绝消息如下:

Your app's location feature is not integrated with the built-in mapping functionality, which limits users to a third-party maps app.您的应用程序的位置功能未与内置地图功能集成,这会将用户限制为第三方地图应用程序。

Next Steps下一步

To resolve this issue, please revise your app to give users the option to launch the native Apple Maps app.要解决此问题,请修改您的应用程序,让用户可以选择启动本机 Apple Maps 应用程序。

I have found that there exists some documentation about a Javascript library named MapKit JS , which serves precisely the purpose of interacting with Apple Maps : https://developer.apple.com/maps/mapkitjs/我发现存在一些关于名为MapKit JS的 Javascript 库的文档,它正好用于与Apple Maps交互的目的: https : //developer.apple.com/maps/mapkitjs/

<script src="https://cdn.apple-mapkit.com/mk/5.x.x/mapkit.js"></script>

<script>
        mapkit.init({
            authorizationCallback: function(done) {
                var xhr = new XMLHttpRequest();
                xhr.open("GET", "/services/jwt");
                xhr.addEventListener("load", function() {
                    done(this.responseText);
                });
                xhr.send();
            }
        });

        var Cupertino = new mapkit.CoordinateRegion(
            new mapkit.Coordinate(37.3316850890998, -122.030067374026),
            new mapkit.CoordinateSpan(0.167647972, 0.354985255)
        );
        var map = new mapkit.Map("map");
        map.region = Cupertino;
        </script>

Nevertheless, I could really use some help no how to connect with this MapKit JS using DART , instead of JAVA, for our Flutter application .尽管如此,对于我们的Flutter 应用程序,我真的可以使用一些帮助,而不是如何使用DART而不是 JAVA 与这个MapKit JS连接。

Thank you immensely for your kind help!非常感谢您的帮助!

Daniel丹尼尔

Firstly, install the url_launcher plugin首先,安装url_launcher插件

Secondly, add the below code in Info.plist:其次,在 Info.plist 中添加以下代码:

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

Thirdly:第三:

var urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';

if (await canLaunch(urlAppleMaps)) {
  await launch(urlAppleMaps);
} else {
  throw 'Could not launch $url';
}

We can use it like this:我们可以这样使用它:


 _launchMap(BuildContext context, lat, lng) async {
    var url = '';
    var urlAppleMaps = '';
    if (Platform.isAndroid) {
      url = "https://www.google.com/maps/search/?api=1&query=${lat},${lng}";
    } else {
      urlAppleMaps = 'https://maps.apple.com/?q=$lat,$lng';
      url = "comgooglemaps://?saddr=&daddr=$lat,$lng&directionsmode=driving";
      if (await canLaunch(url)) {
        await launch(url);
      } else {
        throw 'Could not launch $url';
      }
    }

    if (await canLaunch(url)) {
      await launch(url);
    } else if (await canLaunch(urlAppleMaps)) {
      await launch(urlAppleMaps);
    } else {
      throw 'Could not launch $url';
    }
 }

You could try to use a Map Launcher plugin to launch apple/google maps depending on a platform您可以尝试使用Map Launcher插件根据平台启动苹果/谷歌地图

import 'dart:io';
import 'package:map_launcher/map_launcher.dart';

if (Platform.isIOS) {
  await MapLauncher.launchMap(
    mapType: MapType.apple,
    coords: Coords(31.233568, 121.505504),
    title: "Shanghai Tower",
    description: "Asia's tallest building",
  );
} else {
  await MapLauncher.launchMap(
    mapType: MapType.google,
    coords: Coords(31.233568, 121.505504),
    title: "Shanghai Tower",
    description: "Asia's tallest building",
  );
}

A simple solution I have found is to replace the google maps URL with a maps.apple.com equivalent, using the same url_launcher function.我发现的一个简单解决方案是使用相同的 url_launcher 函数将 google 地图 URL 替换为 maps.apple.com 等效项。

For example, the original function for Google Maps:例如,谷歌地图的原始函数:

void _mapaBethania() async {
    const url = "https://www.google.com/maps/place/Panamanian+Institute+for+Special+Training/@9.0067418,-79.5300556,17z/data=!3m1!4b1!4m5!3m4!1s0x8faca84549395297:0x9c54b1fdb96ac590!8m2!3d9.0067365!4d-79.5278669";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Lo sentimos, no es posible abrir: $url';
    }
  }

Has become:已经成为:

void _mapaBethania() async {
  const url = "https://maps.apple.com/?q=IPHE&ll=9.0067418,-79.5300556&z=16";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Lo sentimos, no es posible abrir: $url';
    }
  }

Nevertheless, although this change does fulfill Apple's enforced obligation to use their own software instead of the competition's, the user experience with Apple Maps is very poor, because once the Map App is shown, it becomes a bit confusing and difficult to return back to the original App.尽管如此,虽然这一变化确实履行了苹果强制要求使用自己的软件而不是竞争对手的软件的义务,但苹果地图的用户体验非常差,因为一​​旦显示地图应用程序,就会变得有点混乱,很难回到原始应用程序。

Therefore, I am planning to write code that enables both options, Apple Maps in order to comply with Apple enforcement, and also Google Maps in order to provide a better user experience, despite Apple.因此,我计划编写代码来启用这两个选项,Apple Maps 以符合 Apple 的规定,以及 Google Maps 以提供更好的用户体验,尽管是 Apple。

Anyway, the latter is just a personal opinion;无论如何,后者只是个人意见; the true fact is that replacing the URL for a maps.apple.com equivalent using the same launch_url function, does seem acceptable to comply with Apple requirements.真实的事实是,使用相同的launch_url 函数替换maps.apple.com 等价物的URL 似乎可以满足Apple 的要求。

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

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