【问题标题】:Navigate to search result Flutter Google Maps导航到搜索结果 Flutter Google Maps
【发布时间】:2020-08-07 17:40:08
【问题描述】:

我有一个 SearchMapPlaceWidget,它根据我的搜索关键字向我推荐地点。我想将相机动画到我点击的建议位置。我该怎么做?

这是我在 Flutter 中的 search_map_place 包中使用的 SearchMapPlaceWidget。

SearchMapPlaceWidget(
  apiKey: myApiKey,
  onSearch: (Place place) async {
    try{
      final geolocation = await place.geolocation;
      print(geolocation);
    }catch(e){
      print(e);
    }
  },
),

【问题讨论】:

    标签: google-maps flutter google-places-api google-geolocation


    【解决方案1】:

    来自官方示例https://pub.dev/packages/search_map_place#example
    您可以使用GoogleMapController 并致电animateCamera

    return SearchMapPlaceWidget(
        apiKey: YOUR_API_KEY,
        // The language of the autocompletion
        language: 'en',
        // The position used to give better recomendations. In this case we are using the user position
        location: userPosition.coordinates,
        radius: 30000,
        onSelected: (Place place) async {
            final geolocation = await place.geolocation;
    
            // Will animate the GoogleMap camera, taking us to the selected position with an appropriate zoom
            final GoogleMapController controller = await _mapController.future;
            controller.animateCamera(CameraUpdate.newLatLng(geolocation.coordinates));
            controller.animateCamera(CameraUpdate.newLatLngBounds(geolocation.bounds, 0));
        },
    );
    

    来自官方的例子有你需要的所有代码
    屏幕顶部有地图和搜索框。当用户通过自动完成选择一个地点时,屏幕移动到所选位置
    https://github.com/Bernardi23/search_map_place/blob/master/example/advanced_example.dart
    你可以直接运行这个例子

    太长了,我只贴_mapController部分

    Completer<GoogleMapController> _mapController = Completer();
    ...
    GoogleMap(
                ...
                onMapCreated: (GoogleMapController controller) async {
                  _mapController.complete(controller);
    ...
    final GoogleMapController controller = await _mapController.future;
    

    【讨论】:

    • 在这个 sn-p 中,_mapController 是什么?因为我有一个名为 _controller 的 GoogleMapController 实例,我通过了它,但它没有未来。
    猜你喜欢
    • 1970-01-01
    • 2017-08-05
    • 2021-03-31
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    相关资源
    最近更新 更多