0

所以我试图在谷歌地图上显示动态标记。我有一组客户的经度和纬度。

import 'package:google_maps_flutter/google_maps_flutter.dart';

class Client {
  int id;
  double longitude;
  double latitude;
  String adresse;
  String nom;
  String prenom;
  String contact;
  int id_compte;
  int id_entreprise;
  GoogleMapController controller;

  Client(int id, double lat, double longi, String adr, String n, String p,
      String con, int compte, int entr) {
    this.id = id;
    this.longitude = longi;
    this.latitude = lat;
    this.adresse = adr;
    this.nom = n;
    this.prenom = p;
    this.contact = con;
    this.id_compte = compte;
    this.id_entreprise = entr;
  }

  Client.fromJson(Map json)
      : id = json['id'],
        longitude = json['longitude'],
        latitude = json['latitude'],
        adresse = json['adresse'],
        nom = json['nom'],
        prenom = json['prenom'],
        contact = json['contact'],
        id_compte = json['id_compte'],
        id_entreprise = json['id_entreprise'];
  Map toJson() {
    return {
      'id': id,
      'longitude': longitude,
      'latitude': latitude,
      'adresse': adresse,
      'nom': nom,
      'prenom': prenom,
      'contact': contact,
      'id_compte': id_compte,
      'id_entreprise': id_entreprise,
    };
  }
}

List<Client> clients = [
  Client(1, 6.801, 1.1202, "Wonyomé", "AMI", "Komi", "93121250", 1, 2),
  Client(1, 6.8055, 1.1241, "Wonyomé", "PAUl", "Komi", "93121250", 2, 2),
  Client(1, 6.820, 1.12012, "Wonyomé", "ElI", "Komi", "93121250", 3, 2),
  Client(1, 6.8001, 1.12100, "Wonyomé", "JAMES", "Komi", "93121250", 4, 2),
  Client(1, 6.8032, 1.12364, "Wonyomé", "ESSE", "Komi", "93121250", 5, 2),
  Client(1, 6.81, 1.12102, "Wonyomé", "AFI", "Komi", "93121250", 6, 2),
  Client(1, 6.810, 1.12004, "Wonyomé", "ESOU", "Komi", "93121250", 7, 2),
];

这就是我如何实现谷歌地图

import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:ordure/models/client.dart' as Client;

class ListCLient extends StatefulWidget {
  const ListCLient({Key key}) : super(key: key);

  @override
  _ListCLientState createState() => _ListCLientState();
}

class _ListCLientState extends State<ListCLient> {
  List<Marker> _markers = [];

  GoogleMapController _controller;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_) async {
      for (var i = 0; i < Client.clients.length; i++) {
        final marker = Marker(
            markerId: MarkerId(Client.clients[i].id.toString()),
            position:
                LatLng(Client.clients[i].latitude, Client.clients[i].longitude),
            infoWindow: InfoWindow(title: Client.clients[i].nom),
            onTap: () {
              showClientInfos(Client.clients[i]);
            }, //showClientInfos(Client.clients[i]),
            icon:
                BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed));
        setState(() {
          _markers.add(marker);
        });
      }
    });
  }

  void _onMapCreated(GoogleMapController _cntlr) {
    _controller = _cntlr;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text('Mes clients de la journée'),
      ),
      body: Stack(
        children: [
          Positioned.fill(
            child: GoogleMap(
                onMapCreated: (controller) {
                  _onMapCreated(controller);
                },
                myLocationEnabled: true,
                mapType: MapType.normal,
                initialCameraPosition:
                    CameraPosition(target: LatLng(6.8024, 1.120211), zoom: 10),
                markers: Set.from(_markers)),
          ),
        ],
      ),
    );
  }
}

问题是只显示一个标记:最后一个客户。我已经将迭代放在_onMapCreated方法中,但仍然是同样的问题。

有谁知道我的代码中缺少什么?

谢谢

4

1 回答 1

0

在您的 initState 中删除该 setState To _markers.add(marker);

于 2021-08-10T16:23:54.580 回答