我已经创建了一个自定义图像,用作我的flutter应用程序上google地图上我的一个标记的标记图标。不幸的是,这不能按计划工作,而是显示默认图标。有人能发现这个错误吗?我当然不能。附带说明一下,控制台中的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 / Parcel(22617):读取此处不支持的NULL字符串。 E / Parcel(22617):此处不支持读取NULL字符串。
答案 0 :(得分:3)
您似乎没有正确检索代码上的标记图标,因为您仅将其定义为restaurantmarker
。这是解决问题的方法:
首先,您需要确保在pubspec.yaml
下的Flutter:
上定义了图标:
Flutter:
assets:
- assets/images/yellow_MarkerR.png
然后,您将需要在BitmapDescriptor.fromAssetImage
内调用initState()
来获取图标,然后再加载地图:
void initState() {
BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/images/yellow_MarkerR.png')
.then((value) => rmarker = value);
}
答案 1 :(得分:2)
您的代码有很多问题,我只是将代码的重要部分重构为地图逻辑。
但是重要的问题是您不能确保restaurantMarker
的初始化。我在此处添加了isSetupReady
;
首先,请确保已将图标资产assets/images/yellow_MarkerR.png
添加到pubsepc.yaml文件中。
import 'package:flutter/material.dart';
class NearbyScreen extends StatefulWidget {
@override
_NearbyScreenState createState() => _NearbyScreenState();
}
class _NearbyScreenState extends State<NearbyScreen> {
var _markers;
var setmarkers;
var restaurantMarker;
bool isSetupReady = false;
@override
void initState() {
doSetup();
super.initState();
}
doSetup() async {
restaurantMarker = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/images/yellow_MarkerR.png');
_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"))
];
setmarkers = _markers.toSet();
setState(() {
isSetupReady = true;
});
}
@override
//LocationHelper.mapviewpointer(latitude: )
Widget build(BuildContext context) {
return /* !_serviceEnabled ? Center(child:Text("Page cannot be viewed"),) :
map == null
? Center(
child: Text("Null response"),
)
:*/
isSetupReady
? 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,
)
: Center(child: Text('Loading Maps...'));
}
}