我的颤振资产图像没有被用作谷歌地图的标记图标
Posted
技术标签:
【中文标题】我的颤振资产图像没有被用作谷歌地图的标记图标【英文标题】:my flutter asset image is not being used as my marker icon for google maps 【发布时间】:2020-10-04 00:11:33 【问题描述】:我创建了一个自定义图像,用作我的标记图标,用于我的颤振应用程序上的谷歌地图上的一个标记。不幸的是,这没有按计划工作,而是显示默认图标。任何人都可以发现 eroor 吗?我当然不能。作为旁注,我的 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): Reading a NULL string not supported here。 E/Parcel (22617):此处不支持读取 NULL 字符串。
【问题讨论】:
【参考方案1】:你的代码有很多问题,我只是重构了地图逻辑代码的重要部分。
但重要的问题是您没有确保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...'));
【讨论】:
【参考方案2】:看起来您没有正确检索代码上的标记图标,因为您仅将其定义为 restaurantmarker
。以下是解决此问题的方法:
首先,您需要确保在pubspec.yaml
的Flutter:
部分下定义了您的图标:
Flutter:
assets:
- assets/images/yellow_MarkerR.png
然后,您需要在地图加载之前调用initState()
内的BitmapDescriptor.fromAssetImage
以获取图标:
void initState()
BitmapDescriptor.fromAssetImage(
ImageConfiguration(), 'assets/images/yellow_MarkerR.png')
.then((value) => rmarker = value);
【讨论】:
以上是关于我的颤振资产图像没有被用作谷歌地图的标记图标的主要内容,如果未能解决你的问题,请参考以下文章