【问题标题】:Show name of the place with Google Directions API使用 Google Directions API 显示地点名称
【发布时间】:2020-09-20 14:04:56
【问题描述】:

我正在尝试使用多个航点向 Google Directions API 发出请求。到目前为止一切正常,但我遇到的主要问题是当谷歌地图应用程序为用户打开时,用户只能看到坐标。

我正在开发一个 Flutter 应用程序,但可能这个请求对于所有设备和平台都是相同的,所以它并不是 Flutter 特定的。

我想显示路线上的公司/地点的名称。

它可以是网络请求,也可以是使用 Google 地图移动应用的 URL 方案的请求。

这就是我目前所做的,带有坐标。


  Future<void> launchGoogleMaps(BuildContext context) async {
    Position userLocation = await Geolocator().getLastKnownPosition(
      desiredAccuracy: LocationAccuracy.bestForNavigation,
    );

    String googleMapsAppUrl = 'comgooglemapsurl://?';

    if (customFirstPlace != null) {
      googleMapsAppUrl +=
          "saddr=${customFirstPlace.latitude},${customFirstPlace.longitude}";
    } else if (!shouldStartWithUserLocation) {
      googleMapsAppUrl +=
          "saddr=${circuitCompanies[0].lat},${circuitCompanies[0].lng}";
    }

    String googleMapsWebUrl = "http://www.google.com/maps/dir";
    googleMapsWebUrl += "/${userLocation.latitude},${userLocation.longitude}";

    circuitCompanies.forEach((CompanyModel element) {
      int elementIndex = circuitCompanies.indexOf(element);
      // If it's the first element, the "direction"
      // parameter will be added to specify the beginning.
      // If not, the "to" parameter will be added
      // to specify a waypoint (a stop).

      if (elementIndex == 0) {
        googleMapsAppUrl += "&daddr=${element.lat},${element.lng}";
      } else {
        googleMapsAppUrl += "+to:${element.lat},${element.lng}";
      }

      googleMapsWebUrl += "/${element.lat},${element.lng}";
    });

    if (await canLaunch("comgooglemaps://")) {
      print('launching com googleUrl $googleMapsAppUrl');
      await launch(googleMapsAppUrl);
    } else if (await canLaunch(googleMapsWebUrl)) {
      print('launching web url $googleMapsWebUrl');
      await launch(googleMapsWebUrl);
    } else {
      showDialog(
        builder: (_) {
          return SingleActionDialog(
            title: "Erreur",
            onConfirmTap: () => Navigator.pop(context),
            bodyText: "Un erreur s'est produit.",
            confirmText: "Fermer",
          );
        },
        context: context,
      );
    }
  }

我怎样才能显示适当的路线,以及用户将去的地方的名称,而不是只显示坐标?我也可以访问确切的地点地址。

谢谢!

【问题讨论】:

    标签: google-maps flutter


    【解决方案1】:

    我设法解决了这个问题。我改为使用 Google Maps API 提供的 Web URL。

    我创建了一个函数,它按照 Google 地图标准返回该地点的地址,而不仅仅是传递坐标。

     Future<String> getEncodedCompanyAddress(CompanyModel model)  async {
    String _encodedAddress = "";
    
    model.address = await CompanyAddressDAO().getCompanyAddress(
      model.id,
    );
    
    _encodedAddress = model.details.title +
        ", " +
        model.address.street +
        ", " +
        model.address.city +
        ", " +
        model.address.state;
    
    _encodedAddress = Uri.encodeFull(_encodedAddress);
    
    _encodedAddress = _encodedAddress.replaceAll("%20", "+");
    
    return _encodedAddress;
    

    }

    现在,实际地名不再只显示坐标,而是显示在 Google 地图应用程序的方向上。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-12
      • 2011-05-24
      • 2023-04-02
      相关资源
      最近更新 更多