gpt4 book ai didi

google-maps - 使用 Flutter 围绕谷歌地图标记中心相机位置

转载 作者:行者123 更新时间:2023-12-03 02:45:52 24 4
gpt4 key购买 nike

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

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

  • 初始设置为静态
  • getMapMarkers()函数,我会调用 setMarkers(List<dynamic> markers) (接受标记或对象的列表)并获取 GeoPoint 之一从标记列表中,并使用它来设置 initialCameraPosition .
     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);
    });
    }
  • 最佳答案

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

      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)));

    关于google-maps - 使用 Flutter 围绕谷歌地图标记中心相机位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305871/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com