Flutter-Google地图,同时有多个不同的自定义标记图像?

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

我正在使用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) {...

然后我创建了Google地图



            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个回答
0
投票

您可以用这样的东西来做

_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');
© www.soinside.com 2019 - 2024. All rights reserved.