微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flutter - Google Map - 标记未显示

如何解决Flutter - Google Map - 标记未显示

我是 Flutter 的新手,即使我在互联网上到处搜索解决方案,我也无法理解这个问题。所以我遇到的问题如下。我加载了地图屏幕,它给了我位置,然后它没有向我显示标记,这真的很烦人,因为一个月前该应用程序运行良好,直到昨天发现错误时我才触摸代码

谁能检查代码,看看我是否遗漏了什么? P.S(我已经有了地图键和所有设置) 谢谢。

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:google_place/google_place.dart';

import 'DetailPlace.dart';
import 'PharmacyDetailScreen.dart';
import 'SendUID.dart';

class MapScreen extends StatefulWidget {
  @override
  _MapScreenState createState() => _MapScreenState();
}

class _MapScreenState extends State<MapScreen> {
  final _firestore = FirebaseFirestore.instance;
  GoogleMapController mapController;
  Set<Marker> markers = Set();
  String UID;

  //Key used to be able to acess the googlePlace
  var googlePlace = GooglePlace("");

  //Attributes of each place
  String name;
  double reting;
  bool openingHours;

  //DetailsResult is an object used to take the result of the search for the places
  DetailsResult detailsResult;

  //this are the longitude and the latitude of the phone
  double lng;
  double lat;

  //List with places
  List<SearchResult> searchResultList = [];
  LatLng _center = LatLng(45.521888,11.319734);

  void saveUID(String uid) {
    this.UID = uid;
  }

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
    getPosition().then((value) => getLocationNearBy());
  }

  void getDetils(String placeId) async {
    var result = await this.googlePlace.details.get(placeId);
    if (result != null && result.result != null && mounted) {
      setState(() {
        detailsResult = result.result;
      });

      /*
      if (result.result.photos != null) {
        for (var photo in result.result.photos) {
          getPhoto(photo.photoReference);
        }
      }*/
    }
  }

  /*
  void getPhoto(String photoReference) async {
    var result = await this.googlePlace.photos.get(photoReference,null,400);
    if (result != null && mounted) {
      print(result);
      setState(() {
        images.add(result);
      });
    }
  }
*/

  //Method used to create the markers that you see on the map
  void CreateMarkers() {
    print("test");
    for (int i = 0; i < searchResultList.length; i++) {
      markers.add(
        Marker(
          infoWindow: InfoWindow(
            title: searchResultList[i].name,),markerId: MarkerId(searchResultList[i].name),position: LatLng(searchResultList[i].geometry.location.lat,searchResultList[i].geometry.location.lng),onTap: () {
            print("2 This is the ID I received ${UID}");
            Navigator.push(
              context,MaterialPageRoute(
                builder: (context) => PharmacyDetailScreen(),settings: RouteSettings(
                  arguments: DetailPlace(
                    searchResultList[i].name,searchResultList[i].rating,this.UID,);
          },);
    }
  }

  //Method used to get the position of the device
  Future<Position> getPosition() async {
    LocationPermission permission = await Geolocator.checkPermission();
    LocationPermission permission2 = await Geolocator.requestPermission();
    Position position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    lat = position.latitude;
    lng = position.longitude;

    return position;
  }

  //Method used to get the pharmacies around my location
  void getLocationNearBy() async {
    var result = await googlePlace.search
        .getNearBySearch(Location(lat: lat,lng: lng),2000,type: "pharmacy");
    setState(() {
      searchResultList = result.results;
    });
    //print(searchResultList[0].openingHours.toString());
    //SaveNametoDataBase();
    print("GetLocationTest");
    CreateMarkers();
  }

/*
  void SaveNametoDataBase() {
    for (int i = 0; i < searchResultList.length; i++) {
      _firestore.collection('Pharmacies').doc(searchResultList[i].name).set({
        'icon': searchResultList[i].icon,'rating': searchResultList[i].rating,});
    }
  }
*/

  @override
  Widget build(BuildContext context) {
    final SendUID sendUID = ModalRoute.of(context).settings.arguments;
    saveUID(sendUID.uid);

    return MaterialApp(
      home: Scaffold(
        body: GoogleMap(
          markers: markers,myLocationEnabled: true,myLocationButtonEnabled: true,onMapCreated: _onMapCreated,initialCameraPosition: CameraPosition(
            target: _center,zoom: 15.0,);
  }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?