android 无法打开Google Maps Turn By Turn导航:Flutter

cnjp1d6j  于 2023-05-27  发布在  Android
关注(0)|答案(1)|浏览(620)

bounty还有4天到期。此问题的答案有资格获得+50声望奖励。M. Usman Khan希望引起更多关注这个问题。

我正在尝试将以下功能集成到我的flutter应用程序中-当用户单击导航按钮时,它应该将用户重定向到GoogleMap应用程序并启动Turn by Turn导航(我们通常使用)
我使用下面的代码-(只是一个示例错误消息,虽然我使用的纬度和经度,这给出了类似的错误)

ElevatedButton(
                              onPressed: () async {
                                String mapsUrl =
                                    'https://www.google.com/maps/dir/?api=1&origin=${appState.position.latitude},${appState.position.longitude}&destination=${riderLocation.latitude},${appState.position.longitude}&travelmode=driving&dir_action=navigate';
                                 Uri uri = Uri.parse(mapsUrl);  
                               
                              
                                if (await canLaunchUrl(mapsUrl)) {
                                  print('can launch url true');
                                  await launchUrl(
                                    Uri.parse(mapsUrl),
                                  );
                                } else {
                                  print('Could not launch url');
                                  throw 'Could not launch $mapsUrl';
                                }
                              },

问题:当我按导航按钮时,GoogleMap会在WebView中打开,显示完整路径和一个选项在应用程序中导航。当我点击它的时候,我得到一个错误信息-

该网页无法访问该网页的目的://maps.app.goo.gl/?link=https://www.google.com/maps/dir/Barcelona,%2BSpain/萨拉戈萨,% 2BSpain/Huesca,%2BSpain/马德里,%2BSpain/@41.2603069,-0.7768843,5z/data%3D!四米十四!四米十三!一米二!1m1!1s0x12a49816718e30e5:0x44b0fb3d4f47660a!1m2!1m1!1s0xd5914dd5e618e91:0x49df13f1158489a8!1m2!1m1!1s0xd584449d1d84db5:0x2c8dfd864aa74791!1m2!1m1!1s0xd422997800a3c81:0xc436dec1618c2269!3e0?entry%3Dml&apn=com.google.android.apps.maps&amv=965100030&isi=585027354&ibi=com.google.Maps&ius=comgooglemapsurl&utm_campaign=ml_promo&ct=ml-dr-wv-msc&mt=8&pt=9008&efr=1#Intent; package=com.google.android.gms; public int getchar =https; S.browser_fallback_url=https://play.google.com/store/apps/details%3Fid%3Dcom.google.android.apps.maps&pcampaignid%3Dfdl_long&url%3Dhttps://www.google.com/maps/dir/Barcelona,%252BSpain/Zaragoza,%252BSpain/Huesca,%252BSpain/Madrid,%252BSpain/@41.2603069,-0.7768843,5z/data%253D!四米十四!四米十三!一米二!1m1!1s0x12a49816718e30e5:0x44b0fb3d4f47660a!1m2!1m1!1s0xd5914dd5e618e91:0x49df13f1158489a8!1m2!1m1!1s0xd584449d1d84db5:0x2c8dfd864aa74791!1m2!1m1!1s0xd422997800a3c81:0xc436dec1618c2269!3e0%3Fentry%253Dml&min_version%3D965100030; end;无法加载,因为:

net::ERR_UNKNOWN_URL_SCHEME

按钮按下时我想要的图像-https://www.google.com/imgres?imgurl=https%3A%2F%2Fi.stack.imgur.com%2Fuy1dU.png&imgrefurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F35967239%2Fandroid-google-map-turn-by-turn-navigation&tbnid=DZTxUO10KEw4_M&vet=12ahUKEwiE4Oji-rf5AhU9i9gFHVOXCeIQMygAegUIARCwAQ..i&docid=vB080_l2mo_iNM&w=480&h=854&q=google%20maps%20turn%20by%20turn%20navigation&ved=2ahUKEwiE4Oji-rf5AhU9i9gFHVOXCeIQMygAegUIARCwAQ

z9smfwbn

z9smfwbn1#

去年,谷歌在iOS和Android上都发生了重大事件,他们改变了它的行为方式。
对于Android,打开实际应用而不是Web表示的唯一有效方法是创建一个针对Google Maps应用的适当Android Intent,而对于iOS,旧的URL启动应该像以前一样工作。区别在于应该启动的URI。
对于Android:

Uri get uri => Uri(
        scheme: 'google.navigation',
        host: '',
        queryParameters: {
          'q': '${destination.latitude},${destination.longitude}',
          'mode': 'd',
        },
      );

    AndroidIntent intent;
    const action = AndroidIntentAction.view;
    // https://stackoverflow.com/a/39037306/1627511
    final flags = [
      Flag.FLAG_ACTIVITY_CLEAR_TASK,
      Flag.FLAG_ACTIVITY_NEW_TASK,
    ];
    intent = AndroidIntent(
      action: action,
      flags: flags,
      data: uri.toString(),
      package: 'com.google.android.apps.maps',
    );
    if (await intent.canResolveActivity() == true) {
        await intent.launch();
      }

对于iOS

Uri get uri => Uri(
        scheme: 'comgooglemaps',
        host: '',
        queryParameters: {
          'daddr': '${destination.latitude},${destination.longitude}',
          'directionsmode': 'driving',
        },
      );
await launchUrlString(uri.toString()); // launch this via https://pub.dev/packages/url_launcher

奖励浏览器

Uri get uri => Uri(
        scheme: 'https',
        host: 'www.google.com',
        path: 'maps/dir/',
        queryParameters: {
          'api': '1',
          'destination': '${destination.latitude},${destination.longitude}',
          'travelmode': 'driving',
          'dir_action': 'navigate',
        },
      );
await launchUrlString(uri.toString()); // launch this via https://pub.dev/packages/url_launcher

希望有帮助

相关问题