首页 > 解决方案 > 在谷歌地图标记上添加一个弹出窗口

问题描述

当使用颤振/飞镖单击或点击时,我将如何添加一个弹出窗口以显示有关谷歌地图上特定标记的详细信息。已尝试使用 onTap 功能,但给了我关于如何实现此功能的任何想法

populateHospitals() {
    Hospitals = []; //array to obtain individual hospital data
    Firestore.instance.collection('markers').getDocuments().then((docs) {
      if (docs.documents.isNotEmpty) {
        setState(() {
          hospitalsToggle = true;
        });
        for (int i = 0; i < docs.documents.length; ++i) {
          Hospitals.add(docs.documents[i].data);
          initMarker(docs.documents[i].data);
        }
      }
    });
  }

  initMarker(hospitals) {
    mapController.clearMarkers().then((val) {
      mapController.addMarker(MarkerOptions(
          position:
              LatLng(hospital['location'].latitude, hospital['location'].longitude),// draws the markers of the hospitals
              draggable: false, // disables dragging of the marker
              consumeTapEvents: true,
              infoWindowText: InfoWindowText(hospital['hospitalname'], 'hospital'), //displays the hospitals name when the icon is clicked

              ));


    });
  }

标签: google-mapsflutter

解决方案


https://github.com/flutter/plugins/blob/master/packages/google_maps_flutter/google_maps_flutter/example/lib/place_marker.dart的源代码中阅读更多内容

 List <Marker> markers = new List<Marker>();

//Run the loop and add locations to marker 


markers.add(
            Marker(
                markerId: <SOMEID>,
                position: LatLng(hospital['location'].latitude, hospital['location'].longitude),


                onTap: () {
                  //this is what you're looking for!
                  print(hospital['hospitalname']);


                }
            )

        );

//Now add this to your map.

推荐阅读