Flutter Google 地图在初始化后不显示带有标签的标记

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

我下面有这段代码,很简单。我正在初始化它并以正确的方式设置它。当我运行此代码时,地图会加载,但带有标签的标记不会加载。但是,如果我在地图加载后热重新加载代码,它会立即出现。所以,我的想法是加载标记标签需要一点时间,所以我将其放入异步标签中但不起作用。如果我使用原始画家方式绘制标签也会发生这种情况。

class mappy extends StatefulWidget {
  const mappy({Key? key});

  @override
  State<mappy> createState() => sst();
}

class sst extends State<mappy> {
  late GoogleMapController mapController;
  late Set<Marker> _markers;
  final Set<Marker> _zipCodeMarkers = {};
  final LatLng _center = const LatLng(42.3314, -83.0458);
  static const MarkerId markerId = MarkerId('Detroit');

  @override
  void initState() {
    super.initState();
    _initZipCodeMarkers(); // Initialize zip code markers
  }

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

   void _initZipCodeMarkers()  {
    _zipCodeMarkers.addLabelMarker(LabelMarker(
      label: 'Static Marker',
      markerId: MarkerId('static_marker'),
      position: LatLng(42.411740, -83.058110),
      backgroundColor: Colors.blue,
      textStyle: TextStyle(
        fontSize: 45, // Adjust the font size as needed
        fontWeight: FontWeight.bold, // Adjust the font weight as needed
        color: Colors.white, // Adjust the text color as needed
      ),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.green,
        useMaterial3: true,
      ),
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Detroit'),
          backgroundColor: Colors.green[700],
        ),
        body: GoogleMap(
          myLocationEnabled: true,
          myLocationButtonEnabled: true,
          compassEnabled: true,
          onMapCreated: _onMapCreated,
          initialCameraPosition: CameraPosition(
            target: _center,
            zoom: 11.5,
          ),
          markers: _zipCodeMarkers,
        ),
      ),
    );
  }
}
flutter dart visual-studio-code maps
1个回答
0
投票

您使用了

label_marker 1.0.1
套餐吗? 我检查了
label_marker 1.0.1
包,它不起作用。 然后我尝试了一些新的东西,例如,
label_google_maps_marker 0.0.3
,效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.