Google maps 在initState()中调用时,flift google maps自定义图标不显示
我已经在我的Flitter应用程序中用google_maps_Flitter 1.0.2创建了一个地图视图,我正在地图视图上设置3个不同的自定义标记。其中两个是使用复选框设置的,但我想在应用程序首次启动时显示第一组自定义标记。使用此代码,我可以在第一次启动应用程序时设置标记,但标记图标是默认图标(类似于红色阳台的图标),而不是我的自定义图标,但如果我点击复选框两次,我将获得带有图标的标记。我甚至尝试使用after_layout library修复此问题,但没有成功Google maps 在initState()中调用时,flift google maps自定义图标不显示,google-maps,flutter,dart,Google Maps,Flutter,Dart,我已经在我的Flitter应用程序中用google_maps_Flitter 1.0.2创建了一个地图视图,我正在地图视图上设置3个不同的自定义标记。其中两个是使用复选框设置的,但我想在应用程序首次启动时显示第一组自定义标记。使用此代码,我可以在第一次启动应用程序时设置标记,但标记图标是默认图标(类似于红色阳台的图标),而不是我的自定义图标,但如果我点击复选框两次,我将获得带有图标的标记。我甚至尝试使用after_layout library修复此问题,但没有成功 import 'dart:as
import 'dart:async';
import 'package:expandable_bottom_sheet/expandable_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:material_design_icons_flutter/material_design_icons_flutter.dart';
import 'package:location/location.dart';
import 'package:after_layout/after_layout.dart';
class RoadAssistance extends StatefulWidget {
@override
_RoadAssistanceState createState() => _RoadAssistanceState();
}
class _RoadAssistanceState extends State<RoadAssistance>
with AfterLayoutMixin<RoadAssistance> {
Completer<GoogleMapController> _controller = Completer();
CameraPosition _currentPosition = CameraPosition(
target: LatLng(6.9147, 79.9729),
zoom: 15.4746,
);
Set<Marker> markers = Set();
Set<Marker> markers1 = Set();
bool askingPermission = false;
bool showGarages = false;
bool showBranches = true;
bool firstTime = true;
LocationData _locationData;
BitmapDescriptor myPosIcon;
BitmapDescriptor branchIcon;
BitmapDescriptor garageIcon;
@override
void initState() {
super.initState();
BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(55, 55)), 'images/me.png')
.then((onValue) {
myPosIcon = onValue;
});
BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(55, 55)), 'images/home-city.png')
.then((onValue) {
branchIcon = onValue;
});
BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(48, 48)), 'images/wrench.png')
.then((onValue) {
garageIcon = onValue;
});
}
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
var padding = MediaQuery.of(context).padding;
return Scaffold(
appBar: AppBar(
title: Text('Road Assitance'),
backgroundColor: Color.fromRGBO(0, 172, 192, 100),
),
body: ExpandableBottomSheet(
background: Stack(children: [
SizedBox(
height: height - 130,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _currentPosition,
myLocationEnabled: true,
myLocationButtonEnabled: true,
markers: markers,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
),
Container(
padding: EdgeInsets.all(5),
width: width,
height: height - 240,
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Column(
children: [
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black38.withOpacity(0.6),
spreadRadius: 1,
blurRadius: 4,
offset:
Offset(0, 3), // changes position of shadow
),
],
),
width: 160,
padding: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
height: 30.0,
width: 30.0,
child: Checkbox(
value: showBranches,
onChanged: (bool newValue) {
newValue
? loadBranches()
: unloadBranches();
setState(() {
showBranches = newValue;
print(newValue);
});
}),
),
Icon(
MdiIcons.homeCity,
color: Colors.green[700],
),
SizedBox(
width: 5,
),
Text(
'Branches',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
)
],
),
),
SizedBox(
height: 10,
),
Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(12),
boxShadow: [
BoxShadow(
color: Colors.black38.withOpacity(0.6),
spreadRadius: 1,
blurRadius: 4,
offset:
Offset(0, 3), // changes position of shadow
),
],
),
width: 160,
padding: EdgeInsets.all(5),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(
height: 30.0,
width: 30.0,
child: Checkbox(
value: showGarages,
onChanged: (bool newValue) {
newValue ? loadGarages() : unloadGarages();
setState(() {
showGarages = newValue;
print(newValue);
});
}),
),
Icon(
MdiIcons.hammerWrench,
color: Colors.orange[700],
),
SizedBox(
width: 5,
),
Text(
'Garages',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
),
)
],
),
),
],
),
],
),
),
]),
),
}
@override
void afterFirstLayout(BuildContext context) {
// Calling the same function "after layout" to resolve the issue.
loadBranches();
print("function called");
}
getLocation() async {
Location location = new Location();
LocationData _locationData1;
bool _serviceEnabled;
PermissionStatus _permissionGranted;
_serviceEnabled = await location.serviceEnabled();
if (!_serviceEnabled) {
_serviceEnabled = await location.requestService();
if (!_serviceEnabled) {
return;
}
}
_permissionGranted = await location.hasPermission();
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = await location.requestPermission();
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
_locationData1 = await location.getLocation();
print(_locationData1);
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(_currentPosition));
markers.add(
Marker(
markerId: MarkerId('I\'m Here'),
position: LatLng(_locationData1.latitude, _locationData1.longitude),
icon: myPosIcon,
),
);
setState(() {
_currentPosition = new CameraPosition(
target: LatLng(_locationData1.latitude, _locationData1.longitude),
zoom: 15.4746,
);
});
}
loadGarages() {
print("laod garages called");
markers.addAll([
Marker(
markerId: MarkerId('SLIC Garage'),
position: LatLng(6.9217, 79.9739),
infoWindow: InfoWindow(title: 'SLIC Garage New'),
icon: garageIcon,
),
Marker(
markerId: MarkerId('SLIC Garage 2'),
position: LatLng(6.9197, 79.9639),
infoWindow: InfoWindow(title: 'SLIC Garage Nugapityiia'),
icon: garageIcon,
),
Marker(
markerId: MarkerId('SLIC Garage 3'),
position: LatLng(6.9087, 79.9729),
infoWindow: InfoWindow(title: 'SLIC Garage '),
icon: garageIcon,
),
]);
}
unloadGarages() {
print("unlaod garages called");
markers.removeWhere((x) => x.icon == garageIcon);
}
loadBranches() {
print("laod branches called");
markers.addAll([
Marker(
markerId: MarkerId('SLIC Branch'),
position: LatLng(6.9167, 79.9749),
infoWindow: InfoWindow(title: 'SLIC Branch Hewagama'),
icon: branchIcon,
),
Marker(
markerId: MarkerId('SLIC Branch 2'),
position: LatLng(6.9157, 79.9639),
infoWindow: InfoWindow(title: 'SLIC Branch Kotahawala'),
icon: branchIcon,
),
Marker(
markerId: MarkerId('SLIC Branch Malabe'),
position: LatLng(6.9287, 79.9729),
icon: branchIcon,
),
]);
}
unloadBranches() {
if (firstTime) {
markers.clear();
firstTime = false;
return;
}
print("unlaod branches called");
markers.removeWhere((x) => x.icon == branchIcon);
}
}
导入'dart:async';
导入“包:可扩展的底部\u表/可扩展的底部\u表.dart”;
进口“包装:颤振/材料.省道”;
导入“包:google_-maps_-flatter/google_-maps_-flatter.dart”;
导入“包装:材料\设计\图标\颤振/材料\设计\图标\颤振.dart”;
导入“package:location/location.dart”;
导入“package:after_layout/after_layout.dart”;
类道路援助扩展了StatefulWidget{
@凌驾
_道路辅助状态createState()=>\u道路辅助状态();
}
道路辅助状态扩展状态
与AfterLayoutMixin{
Completer _controller=Completer();
CameraPosition _currentPosition=CameraPosition(
目标:LatLng(6.9147,79.9729),
缩放:15.4746,
);
Set markers=Set();
Set markers1=Set();
bool askingPermission=false;
bool showGarages=false;
bool=true;
bool firstTime=true;
位置数据_位置数据;
位图描述符myPosIcon;
位图描述符;
位图描述符garageIcon;
@凌驾
void initState(){
super.initState();
BitmapDescriptor.fromAssetImage(
ImageConfiguration(大小:size(55,55)),'images/me.png')
.然后((onValue){
myPosIcon=onValue;
});
BitmapDescriptor.fromAssetImage(
ImageConfiguration(大小:size(55,55)),'images/home city.png')
.然后((onValue){
branchIcon=onValue;
});
BitmapDescriptor.fromAssetImage(
ImageConfiguration(大小:大小(48,48)),'images/扳手.png')
.然后((onValue){
garageIcon=onValue;
});
}
@凌驾
小部件构建(构建上下文){
double width=MediaQuery.of(context).size.width;
double height=MediaQuery.of(context).size.height;
var padding=MediaQuery.of(context).padding;
返回脚手架(
appBar:appBar(
标题:文本(“道路援助”),
背景颜色:颜色。来自RGBO(0172192100),
),
主体:可膨胀底板(
背景:堆栈(子项:[
大小盒子(
高度:高度-130,
孩子:谷歌地图(
mapType:mapType.normal,
initialCameraPosition:_currentPosition,
myLocationEnabled:对,
myLocationButtonEnabled:正确,
标记:标记,
onMapCreated:(谷歌地图控制器){
_控制器。完成(控制器);
},
),
),
容器(
填充:边缘设置。全部(5),
宽度:宽度,
高度:高度-240,
子:列(
crossAxisAlignment:crossAxisAlignment.end,
mainAxisAlignment:mainAxisAlignment.spaceBetween,
儿童:[
纵队(
儿童:[
容器(
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径。圆形(12),
boxShadow:[
箱形阴影(
颜色:颜色。黑色38。不透明度(0.6),
扩展半径:1,
半径:4,
抵消:
偏移量(0,3),//更改阴影的位置
),
],
),
宽度:160,
填充:边缘设置。全部(5),
孩子:排(
mainAxisAlignment:mainAxisAlignment.start,
儿童:[
大小盒子(
身高:30.0,
宽度:30.0,
子项:复选框(
价值:showBranchs,
一旦更改:(bool newValue){
新价值
?负载支路()
:UnloadBranchs();
设置状态(){
showBranchs=newValue;
打印(新值);
});
}),
),
图标(
我的家乡,
颜色:颜色。绿色[700],
),
大小盒子(
宽度:5,
),
正文(
“分支机构”,
样式:TextStyle(
尺寸:16,
fontWeight:fontWeight.w600,
),
)
],
),
),
大小盒子(
身高:10,
),
容器(
装饰:盒子装饰(
颜色:颜色,白色,
边界半径:边界半径
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
myPosIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(55, 55)), 'images/me.png');
branchIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(55, 55)), 'images/home-city.png');
garageIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(size: Size(48, 48)), 'images/wrench.png');
setState(() {});
});
super.initState();
}