我的flutter资产图像没有被用作我的标记图标为谷歌Map

arknldoa  于 2023-06-24  发布在  Flutter
关注(0)|答案(3)|浏览(156)

我已经创建了一个自定义的图像,作为我的标记图标,我的谷歌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字符串。

k10s72fa

k10s72fa1#

看起来您没有正确地检索代码上的标记图标,因为您只将其定义为restaurantmarker。以下是您可以解决此问题的方法:
首先,您需要确保您已经在pubspec.yamlFlutter:部分下定义了图标:

Flutter:
    assets:
      - assets/images/yellow_MarkerR.png

然后,你需要调用initState()中的BitmapDescriptor.fromAssetImage,以在Map加载之前获取图标:

void initState() {
     BitmapDescriptor.fromAssetImage(
        ImageConfiguration(), 'assets/images/yellow_MarkerR.png')
     .then((value) => rmarker = value);
}
h22fl7wq

h22fl7wq2#

你的代码有很多问题,我只是重构了你的代码中重要的部分,用于Map逻辑。
但重要的问题是您没有确保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...'));
  }
}

相关问题