导航到搜索结果翩翩谷歌地图

问题描述 投票:0回答:1

我有一个SearchMapPlaceWidget,它可以根据我的搜索关键字向我推荐一些地方。我想让相机对我点击的建议地点产生动画。我该怎么做呢?

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

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个回答
0
投票

来自官方示例 https:/pub.devpackagessearch_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.comBernardi23search_map_placeblobmasterexampleadvanced_example.dart。 你可以直接运行这个例子

太长了,我只是粘贴 _mapController 部分

Completer<GoogleMapController> _mapController = Completer();
...
GoogleMap(
            ...
            onMapCreated: (GoogleMapController controller) async {
              _mapController.complete(controller);
...
final GoogleMapController controller = await _mapController.future;
© www.soinside.com 2019 - 2024. All rights reserved.