【问题标题】:Slow Google Maps Navigation Launching on Flutter在 Flutter 上启动缓慢的 Google 地图导航
【发布时间】:2020-04-08 07:17:48
【问题描述】:

在我的应用程序中,用户可以启动由谷歌地图应用程序上显示的当前位置和目的地组成的导航。

代码如下:

 ListTile(
                    title:
                        Text("Launch Google Maps", textAlign: TextAlign.center),
                    onTap: () async {
                      Position position = await Geolocator().getCurrentPosition(
                          desiredAccuracy: LocationAccuracy.bestForNavigation);

                      print("Position:  $position"); //all Information about position (Coordinates etc.)

                      List<Placemark> placemark =
                          await Geolocator().placemarkFromPosition(position);

                      print("Placemark: ${placemark[0].locality}"); //Kingston
                      final String clubName =
                          clubs[index].name.toString().replaceAll(" ", "+");
                      print("Name: $clubName"); //Flawless
                      final String clubCity =
                          clubs[index].location.city.toString().replaceAll("ü", "ue");
                      print("Stadt: $clubCity");//London
                      final String newGoogleUrl =
                          "https://www.google.com/maps/dir/?api=1&origin=${placemark[0].locality}&destination=$clubName,$clubCity";

                      if (await canLaunch(newGoogleUrl)) {
                        await launch(newGoogleUrl);
                      } else {
                        throw 'Could not launch $newGoogleUrl';
                      }
                    },
                  ),

代码流程:

  • 列表项
  • 获取用户当前位置
  • 根据位置获取地点详细信息
  • 获取俱乐部名称
  • 获得俱乐部城市
  • 撰写网址字符串
  • 根据 URL 启动谷歌地图

一切正常。我可以获得所有必需的信息并在最后启动谷歌地图。但这里最大的问题是,这需要大约 8 秒才能启动谷歌地图。我知道这是一个异步函数,我必须等待位置等,但我不知道如何解决这个问题。

如何改进此代码以几乎立即启动谷歌地图,而无需在按下按钮后等待 8 秒?

我考虑过将当前位置存储在共享首选项中,因此我不需要调用 Geolocator API,但我不确定这种方法在多久更新一次该位置等方面。

非常感谢任何帮助。

【问题讨论】:

  • 您解决了这个问题吗?

标签: google-maps flutter async-await geolocation


【解决方案1】:

除了async,你试过精简版的谷歌地图吗?
youtube link
如果您的地图有很多标记,或者只是为了提高性能以换取精美的动画,则它是合适的。
在flutter中,将lite模式设置为true

return GoogleMap(
  liteModeEnabled: true,
  mapType: MapType.normal,
  onMapCreated: _onMapCreated,
  initialCameraPosition: CameraPosition(
    target: _initialLocation,
    zoom: 17,
  ),
  markers: _markers,
  polylines: _lines,
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-18
    • 2013-10-14
    • 2011-03-31
    • 2012-02-08
    • 2020-04-28
    • 2012-03-24
    • 1970-01-01
    • 2020-12-21
    相关资源
    最近更新 更多