【问题标题】:Center Camera Position around Google Maps Markers using Flutter使用 Flutter 围绕谷歌地图标记中心相机位置
【发布时间】:2019-10-11 20:05:50
【问题描述】:

我正在使用google_maps_flutter: ^0.5.13 开发一个基本的“谷歌地图上的多个标记”类型的 Flutter 屏幕。

我很好奇其他人如何为GoogleMap() 类设置initialCameraPostion。我要么

  1. 最初设置为静态
  2. getMapMarkers() 函数中,我将调用setMarkers(List<dynamic> markers)(接收标记或对象列表)并从标记列表中获取GeoPoint 之一并使用它来设置@987654327 @。

     class MapScreenState extends State<MapScreen> {
    
      GoogleMapController _mapController;
    
      // Method #1
      static CameraPosition _initialCameraPosition =
          CameraPosition(target: const LatLng(26.357540, -81.785290), zoom: 12);
    
      void _onMapCreated(GoogleMapController controller) {
        _mapController = controller;
        _getMapMarkers();
      }
    
      @override
      Widget build(BuildContext context) {
        return Stack(children: <Widget>[_googleMap(context)]);
      }
    
      Widget _googleMap(BuildContext context) {
        return Column(children: <Widget>[
          Expanded(
            child: Container(
              height: MediaQuery.of(context).size.height,
              width: MediaQuery.of(context).size.width,
              child: GoogleMap(
                initialCameraPosition: _initialCameraPosition,
                onMapCreated: _onMapCreated,
                mapType: MapType.normal,
                markers: Set<Marker>.of(markers.values),
              ),
            ),
          )
        ]);
      }
    }
    
    // Method #2
    void _setCenter(List<dynamic> markers) {
      GeoPoint geo = markers[0].geocode;
      setState(() {
        _initialCameraPosition =
            CameraPosition(target: LatLng(geo.latitude, geo.longitude), zoom: 12);
      });
    }
    

【问题讨论】:

  • 嗨!有同样的问题。你有没有任何可行的解决方案?

标签: google-maps flutter dart


【解决方案1】:

initialCameraPosition 仅适用于初始时间。您可以使用以下方式移动相机。我们已经有了一个使用onMapCreated hook 的相机控制器。

  void _onMapCreated(GoogleMapController controller) {
    _mapController = controller;
    _getMapMarkers();
  }

使用这个_mapController,我们可以访问moveCamera函数并将相机移动到标记位置。

_mapController.moveCamera(CameraUpdate.newLatLng(<LatLng one of your marker position>));

例子:

_mapController.moveCamera(CameraUpdate.newLatLng(LatLng(markerLat,markerLng)));

【讨论】:

  • 我使用此代码将我的相机位置更新为我当前的 GPS 位置...它成功了,谢谢
  • 您也可以使用 _mapController.animateCamera(...) 以获得更流畅的移动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-27
  • 2021-07-10
  • 2011-03-13
  • 1970-01-01
  • 2017-12-22
相关资源
最近更新 更多