Flutter Map标记未显示 [英] Flutter Map markers not displaying

查看:101
本文介绍了Flutter Map标记未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';

class NewMap extends StatefulWidget {
  @override
  _NewMapState createState() => _NewMapState();
}

class _NewMapState extends State<NewMap> {
  GoogleMapController _controller;

  Position position;

  Widget _child;

  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};
  BitmapDescriptor pinLocationIcon;

  @override
  void initState() {
    _child = SpinKitRipple(
      itemBuilder: (BuildContext context, int index) {
        return DecoratedBox(
          decoration: BoxDecoration(
            color: index.isEven ? Colors.grey : Color(0xffffb838),
          ),
        );
      },
    );
    getCurrentLocation();
    populateClients();
    setCustomMapPin();
    super.initState();
  }

  void getCurrentLocation() async {
    Position res = await getCurrentPosition();
    setState(() {
      position = res;
      _child = mapWidget();
    });
  }

  populateClients() {
    
    FirebaseFirestore.instance.collection('map').get().then((docs) {
      if (docs.docs.isNotEmpty) {
        for (int i = 0; i < docs.docs.length; ++i) {
          initMarker(docs.docs[i].data(), docs.docs[i].id);
           
        }
      }
      
    });
  }

  void initMarker(tomb, tombId) {
    var markerIdVal = tombId;
    final MarkerId markerId = MarkerId(markerIdVal);
    
    final Marker marker = Marker(
      markerId: markerId,
      position: LatLng(tomb['location'].latitude, tomb['location'].longitude),
      icon: pinLocationIcon,
      
    );
    setState(() {
      markers[markerId] = marker;
    });
  }

  void setCustomMapPin() async {
    pinLocationIcon = await BitmapDescriptor.fromAssetImage(
        ImageConfiguration(devicePixelRatio: 2.5), 'assets/images/pin.png');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: IconThemeData(color: Color(0xffffb838)),
        centerTitle: true,
        backgroundColor: Colors.white,
      ),
      body: _child,
    );
  }

  Widget mapWidget() {
    return Stack(
      children: <Widget>[
        GoogleMap(
            initialCameraPosition: CameraPosition(
              target: LatLng(position.latitude, position.longitude),
              zoom: 10,
            ),
            markers: Set<Marker>.of(markers.values),
            onMapCreated: (GoogleMapController controller) {
              _controller = controller;
            },
            compassEnabled: true,
            myLocationEnabled: true,
            
            ),
        SizedBox(
          height: 26,
        ),
      ],
    );
  }
}

这是我的代码,当我尝试在控制台上打印lat long值时将其打印出来,以便从firestore中读取这些值.该地图正确显示了当前位置,同时也正在显示.据我了解,标记的创建是因为我可以将经纬度长的文字打印到控制台上但是标记未显示在地图上.

This is my code when i try to print on the console the lat long values are printed so that the values are read from the firestore. The map is properly displaying the current location is also being displayed. As i understand the markers are been created as i can get the lat long printed to the console But the markers are not displaying on the map.

我该如何解决这个问题

推荐答案

诀窍在于IDE,这要归功于Muthu Thavamani,我能够识别出代码中没有错误.

The trick is with the IDE, thanks to Muthu Thavamani I was able to identify that there are no bugs in the code.

它是由于应用程序的内置而引起的,在将其删除后,问题已得到解决

It happens due to the built of of the application, after removing it the issue is being solved

尝试

flutter clean 命令

这篇关于Flutter Map标记未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆