google-maps - Flutter - Google Maps,同时有多个不同的自定义标记图像?
问题描述
我正在使用 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() {...
}
解决方案
你可以用这样的东西来做
_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');
推荐阅读
- android - 使用 gstreamer 在 android 上录制音频(使用 Qt5)
- ruby-on-rails - 使用 css 格式化生成的按钮
- computer-vision - 用于使线条更细的计算机视觉算法
- android - Flutter(Dart) 中方法的默认返回类型是什么?
- powershell - 有人可以解释为什么这不起作用VS这个
- angular - 如何防止 Angular 中用户角色的操作
- java - 使用 Jackson 数据格式 XML 到 Java 对象的 XML
- javascript - 检查 React 中的对象数组中是否存在相同的值
- spring-boot - Spring是否使“调用自己的控制器”多线程?
- c++ - 将右值作为右值转发是用例吗?