【问题标题】:Flutter - Google Maps, More than one different custom marker Image at the same time?Flutter - Google Maps,同时有多个不同的自定义标记图像?
【发布时间】:2020-07-14 07:20:30
【问题描述】:

我正在使用 Flutter 和 Google Maps API。 我设法在地图打开时显示了一个自定义标记。 有没有办法在同一张地图上同时拥有多个不同的自定义标记图像?

我找不到这样做的方法。 欢迎任何想法或链接:)

class Neighborhood extends StatefulWidget {
  const Neighborhood({Key key}) : super(key: key);
  @override
  _NeighborhoodState createState() => _NeighborhoodState();
}

class _NeighborhoodState extends State<Neighborhood> {
  Location _location = Location();
  GoogleMapController _controller;
  List<Marker> allMarkers = [];
  PageController _pageController;
  int prevPage;
  int bottomSelectedIndex = 0;
//initialising the custom pinIcon
  BitmapDescriptor pinIcon;

  @override
  void initState() {
    super.initState();
//calling the the function that will await the pinIcon and have it ready with initState();
    setCustomMapPin();

    _pageController = PageController(initialPage: 1, viewportFraction: 0.8)
      ..addListener(_onScroll);
  }

  void _onScroll() {...

  _myPlacesList(index) {...

然后我创建了谷歌地图



            child: GoogleMap(
              initialCameraPosition: CameraPosition(
                target: LatLng(40.505757, 22.846576),
                zoom: 12.0,
              ),
              onMapCreated: mapCreated,
              myLocationEnabled: true,
              myLocationButtonEnabled: true,
              mapToolbarEnabled: false,
              markers: Set.from(allMarkers),
            ),
          ),

  }
void setCustomMapPin() async {
    pinIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5), 'assets/images/iconMap.png');
  }

  void mapCreated(controller) {
    setState(() {
      _controller = controller;
      _location.getLocation();
//Adding markers to the screen.Calling the markers from different file.
      myPlaces.forEach((e) {
        allMarkers.add(Marker(
          markerId: MarkerId(e.name),
          draggable: false,
          icon: pinIcon,
          infoWindow: InfoWindow(
            title: e.name,
            snippet: e.address,
          ),
          position: e.locationCoords,
          onTap: () {
            _pageController.animateToPage(myPlaces.indexOf(e),
                duration: Duration(milliseconds: 300), curve: Curves.ease);
          },
        ));
      });
    });
  }
//moves the camera to pin location
  void moveCamera() {...
}

【问题讨论】:

    标签: google-maps flutter google-maps-markers marker


    【解决方案1】:

    你可以这样做

    _markers.add(Marker(
          consumeTapEvents: true,
          position: _center,
          infoWindow: InfoWindow(
              title: 'New Marker',
              snippet: '',
          ),
          icon: markerImage, //you custom marker, instance of BitmapDescriptor
    )
    

    然后你实例化地图:

    GoogleMap(
        onMapCreated: (GoogleMapController controller) {
            mapController = controller;
        },
        myLocationEnabled: locationEnable,
        initialCameraPosition: CameraPosition(
             target: _center,
             zoom: 10.0,
        ),
        mapType: MapType.normal,
        markers: _markers,
    )
    
    
    
     var markerImage = await BitmapDescriptor.fromAssetImage(
              ImageConfiguration(size: Size(96, 96)),
              'assets/image/marker_image.png');
    

    【讨论】:

    • 感谢您的回复。这也是我所做的。现在的问题是如何在同一张地图中拥有不同的自定义标记图标。
    • 每次添加标记时,都需要为每个标记指定不同的markerImage。你试过了吗?
    • 这是我不知道该怎么做的事情。你能告诉我markerImage的代码吗?谢谢
    • 更新答案。
    • 感谢您的帮助!我真的把我的问题说错了,让你明白我为什么要这样做。我已经用代码更新了我的问题。你发的也是我做的。问题是 markerImage 怎么可能是“动态的”,并且每个不同的标记都有不同的图像。
    猜你喜欢
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    相关资源
    最近更新 更多