我已经创建了一个自定义的图像,作为我的标记图标,我的谷歌Map在我的flutter应用程序上。不幸的是,这并没有按计划工作,而是显示默认图标。有人能认出那个人吗?我当然不能。顺便说一句,我的if语句中没有任何内容被打印在控制台中。另一天的问题?
这是我用来设置标记器的代码:
var map;
var rmarker;
final restaurantmarker = BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/images/yellow_MarkerR.png')
.then((value) => rmarker = value);
final mapp = location.getLocation().then((value) => map = value);
final _markers = [
Marker(
markerId: MarkerId("my_location"),
position: LatLng(41.16599, -110.75792),
infoWindow: InfoWindow(title: "YOUR HOME"),
),
Marker(
markerId: MarkerId("RESTAURANT"),
icon: rmarker,
position: LatLng(40.16599, -110.75792),
infoWindow: InfoWindow(title: "Restaurant"))
];
final setmarkers = _markers.toSet();
class NearbyScreen extends StatelessWidget {
void initState() {
startService();
}
@override
//LocationHelper.mapviewpointer(latitude: )
Widget build(BuildContext context) {
return /* !_serviceEnabled ? Center(child:Text("Page cannot be viewed"),) :
map == null
? Center(
child: Text("Null response"),
)
:*/
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(41.16599,
-110.75792 /*map.latitude, map.longitude
double.parse(coordinates[0]), double.parse(coordinates[1]) */
)),
//minMaxZoomPreference: MinMaxZoomPreference(10, 20),
zoomControlsEnabled: true,
markers: setmarkers,
);
}
}
这是完整的代码:
Future<bool> assignService(Location loc) async {
bool servicestatus = await loc.serviceEnabled();
print("Service status $servicestatus");
return servicestatus;
}
Future<PermissionStatus> assignPermission(Location loc) async {
var hasPermission = await loc.hasPermission();
print("Permission status $hasPermission");
return hasPermission;
}
Location location = Location();
var _serviceEnabled;
var _serve = assignService(location).then((value) => _serviceEnabled = value);
//var _permissionGranted = assignPermission(location);
var _permissionGranted;
var _permi =
assignPermission(location).then((value) => _permissionGranted = value);
void startService() {
if (!_serviceEnabled) {
_serviceEnabled = assignService(location);
print("service disabled");
if (!_serviceEnabled) {
return;
}
}
if (_permissionGranted == PermissionStatus.denied) {
_permissionGranted = assignPermission(location);
print("permission denied");
if (_permissionGranted != PermissionStatus.granted) {
return;
}
}
}
var map;
var rmarker;
final restaurantmarker = BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/images/yellow_MarkerR.png')
.then((value) => rmarker = value);
final mapp = location.getLocation().then((value) => map = value);
final _markers = [
Marker(
markerId: MarkerId("my_location"),
position: LatLng(41.16599, -110.75792),
infoWindow: InfoWindow(title: "YOUR HOME"),
),
Marker(
markerId: MarkerId("RESTAURANT"),
icon: rmarker,
position: LatLng(40.16599, -110.75792),
infoWindow: InfoWindow(title: "Restaurant"))
];
final setmarkers = _markers.toSet();
class NearbyScreen extends StatelessWidget {
void initState() {
startService();
}
@override
//LocationHelper.mapviewpointer(latitude: )
Widget build(BuildContext context) {
return /* !_serviceEnabled ? Center(child:Text("Page cannot be viewed"),) :
map == null
? Center(
child: Text("Null response"),
)
:*/
GoogleMap(
initialCameraPosition: CameraPosition(
target: LatLng(41.16599,
-110.75792 /*map.latitude, map.longitude
double.parse(coordinates[0]), double.parse(coordinates[1]) */
)),
//minMaxZoomPreference: MinMaxZoomPreference(10, 20),
zoomControlsEnabled: true,
markers: setmarkers,
);
}
}
我也在我的终端中得到一个错误,它说:E/包裹(22617):此处不支持阅读NULL字符串。E/包裹(22617):此处不支持阅读NULL字符串。
3条答案
按热度按时间k10s72fa1#
看起来您没有正确地检索代码上的标记图标,因为您只将其定义为
restaurantmarker
。以下是您可以解决此问题的方法:首先,您需要确保您已经在
pubspec.yaml
的Flutter:
部分下定义了图标:然后,你需要调用
initState()
中的BitmapDescriptor.fromAssetImage
,以在Map加载之前获取图标:h22fl7wq2#
你的代码有很多问题,我只是重构了你的代码中重要的部分,用于Map逻辑。
但重要的问题是您没有确保
restaurantMarker
的初始化。我在这里添加了一个检查isSetupReady
;首先,确保已将图标资源
assets/images/yellow_MarkerR.png
添加到pubsepc.yaml文件中。vsmadaxz3#
aqui consegui una forma de agregar imagenes al mapa,por si te sirve,https://www.geeksforgeeks.org/how-to-add-custom-markers-on-google-maps-in-flutter/