【问题标题】:Place Multiple Markers - Google Maps API - Flutter/Dart放置多个标记 - Google Maps API - Flutter/Dart
【发布时间】:2021-09-08 01:51:16
【问题描述】:

我有一个带有 GoogleMap 的简单 Flutter 应用程序。 我想要做的是,当单击按钮时,我的数组中的所有标记都会显示在地图上。

注意 - 我能够从 Google API 收集和存储所有与地点相关的信息,并且可以使用它。数据不是问题。唯一的问题是显示我的数组中的标记。

我有 2 组代码,(1) GoogleMap 集成和 (2) 用于创建标记并将每个标记推送到我的数组的代码。

(1)

Container(
                child: GoogleMap(
                  markers: Set<Marker>.from(multipleMarkers),
                  polylines: polylines,
                  trafficEnabled: false,
                  rotateGesturesEnabled: true,
                  buildingsEnabled: true,
                  myLocationEnabled: true,
                  myLocationButtonEnabled: false,
                  mapType: MapType.normal,
                  zoomControlsEnabled: false,
                  initialCameraPosition: CameraPosition(target:currentLatLng, zoom: 15),
                  onMapCreated: (GoogleMapController controller) {
                    _controller.complete(controller);
                    setPolylines();
                  },
                ),
              ),

(2) 该方法链接到一个按钮。

addMultipleMarkers(LatLng latLng) {
    //this method finds the total number of Lat-Lngs in the array -> 20 Lat-Lngs exist
    print("'addMultipleMarkers()' ====================> ${nearbySearchResult.length}");

    for (var i in nearbySearchResult){
      double? lat = 0;
      double? lng = 0;
      if (i.geometry!.location!.lat != null){
        lat = i.geometry!.location!.lat;
      }
      if (i.geometry!.location!.lng != null){
        lng = i.geometry!.location!.lng;
      }

        //creating marker, adding info, and pushing it to 'multitpleMarkers' array
        Marker marker = Marker(
          markerId: MarkerId(i.geometry!.location.toString()),
          position: LatLng(lat!, lng!),
          infoWindow: InfoWindow(
              title: 'Origin',
              snippet: "originSnippet"
          ),
          icon: BitmapDescriptor.defaultMarker,
        );

        //I tried to setState each time marker is added in array - not working
        setState(() {
          multipleMarkers.add(marker);
        });
      }
    //this print line confirms that 20 marks have have been successfully stored in 'multipleMarkers' array
    print("============> TOTAL MARKERS " + multipleMarkers.length.toString());
  }

【问题讨论】:

    标签: flutter google-maps dart flutter-layout google-maps-markers


    【解决方案1】:

    也许使用布尔值来使用您的集合或空集合:

    markers: _isMarkersShowed ? Set<Marker>.from(multipleMarkers) : {}, // not sure if {} is an empty Set, if it doesn't work try with [].
    

    以及显示/隐藏标记的代码(点击按钮时调用此函数):

    showHideMarkers(){
        setState(()=> _isMarkersShowed = !_isMarkersShowed);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-01-26
      • 2013-12-26
      相关资源
      最近更新 更多