颤动的谷歌地图无法显示来自firebase的标记
Posted
技术标签:
【中文标题】颤动的谷歌地图无法显示来自firebase的标记【英文标题】:flutter google map cannot show markers from firebase 【发布时间】:2020-11-24 05:13:34 【问题描述】:我正在学习颤振并尝试使用从 firebase 获得的标记创建谷歌地图。问题是下面的代码只能显示没有标记的地图。从输出中可以看出,可以从firebase成功获取geopoint数据。
// home.dart
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
// import 'package:firebase_analytics/firebase_analytics.dart';
// import 'package:firebase_auth/firebase_auth.dart';
class Home extends StatefulWidget
@override
_HomeState createState() => _HomeState();
class _HomeState extends State<Home>
bool mapToggle = false;
GoogleMapController mapController;
Position _currentPosition;
List<Marker> allMarkers = [];
var clients = [];
_getCurrentLocation() async
Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position)
setState(()
_currentPosition = position;
mapToggle = true;
_populateClients();
);
).catchError((e)
print(e);
);
_populateClients()
CollectionReference ref = FirebaseFirestore.instance.collection('markers');
ref.get().then((doc)
if (doc.docs.isNotEmpty)
for (int i = 0; i < doc.docs.length; i++)
print('$i');
print(doc.docs[i].data());
print(doc.docs[i].data()['clientName']);
print(doc.docs[i].data()['location'].latitude);
print(doc.docs[i].data()['location'].longitude);
allMarkers.add(Marker(
markerId: MarkerId(doc.docs[i].data()['clientName']),
position: LatLng(doc.docs[i].data()['location'].latitude, doc.docs[i].data()['location'].longitude),
draggable: false,
));
);
@override
void initState()
// TODO: implement initState
super.initState();
_getCurrentLocation();
void _onMapCreated(controller)
setState(()
mapController = controller;
);
@override
Widget build(BuildContext context)
return Scaffold(
appBar: AppBar(
title: Text('Home'),
),
body: mapToggle
? GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(
_currentPosition.latitude, _currentPosition.longitude),
zoom: 10.0),
markers: Set.from(allMarkers),
)
: Center(
child: Text(
'Map Loading.. Please wait..',
style: TextStyle(fontSize: 20.0),
)));
我遵循本教程,但许多代码已被弃用: https://youtu.be/Hg8CT3ysFjY
下面是运行选项卡的输出:
Launching lib/main.dart on ONEPLUS A6003 in debug mode...
Running Gradle task 'assembleDebug'...
✓ Built build/app/outputs/flutter-apk/app-debug.apk.
Waiting for ONEPLUS A6003 to report its views...
Debug service listening on ws://127.0.0.1:43105/bLGH86hwpxI=/ws
Syncing files to device ONEPLUS A6003...
W/DynamiteModule(30614): Local module descriptor class for providerinstaller not found.
I/DynamiteModule(30614): Considering local module providerinstaller:0 and remote module providerinstaller:0
W/ProviderInstaller(30614): Failed to load providerinstaller module: No acceptable module found. Local version is 0 and remote version is 0.
I/ter_geolocatio(30614): The ClassLoaderContext is a special shared library.
I/ter_geolocatio(30614): The ClassLoaderContext is a special shared library.
V/NativeCrypto(30614): Registering com/google/android/gms/org/conscrypt/NativeCrypto's 288 native methods...
.
.
.
W/ter_geolocatio(30614): Found duplicate classes, falling back to extracting from APK : /data/user_de/0/com.google.android.gms/app_chimera/m/000001dd/MapsDynamite.apk
W/ter_geolocatio(30614): NOTE: This wastes RAM and hurts startup performance.
W/ter_geolocatio(30614): Found duplicated class when checking oat files: 'Landroidx/annotation/Keep;' in /data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk and /data/user_de/0/com.google.android.gms/app_chimera/m/000001dd/MapsDynamite.apk
W/ter_geolocatio(30614):
W/Gralloc3(30614): allocator 3.x is not supported
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
I/Google Maps Android API(30614): Google Play services client version: 12451000
I/Google Maps Android API(30614): Google Play services package version: 204217037
.
.
.I/flutter (30614): 0
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putInt(Ljava/lang/Object;JI)V (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed)
I/flutter (30614): clientName: 5507, location: Instance of 'GeoPoint'
I/flutter (30614): 5507
I/flutter (30614): 22.30797605
I/flutter (30614): 114.2287517
I/flutter (30614): 1
I/flutter (30614): clientName: 5515, location: Instance of 'GeoPoint'
I/flutter (30614): 5515
I/flutter (30614): 22.2783769
I/flutter (30614): 114.1789305
I/flutter (30614): 2
I/flutter (30614): clientName: 5517, location: Instance of 'GeoPoint'
I/flutter (30614): 5517
I/flutter (30614): 22.29564323
I/flutter (30614): 114.1717511
I/flutter (30614): 3
I/flutter (30614): clientName: 5516, location: Instance of 'GeoPoint'
I/flutter (30614): 5516
I/flutter (30614): 22.27870029
I/flutter (30614): 114.1839518
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ConnectionTracker(30614): Exception thrown while unbinding
W/ConnectionTracker(30614): java.lang.IllegalArgumentException: Service not registered: lp@df9eea1
W/ConnectionTracker(30614): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1766)
W/ConnectionTracker(30614): at android.app.ContextImpl.unbindService(ContextImpl.java:1810)
W/ConnectionTracker(30614): at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
W/ConnectionTracker(30614): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):1)
W/ConnectionTracker(30614): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):2)
W/ConnectionTracker(30614): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):10)
W/ConnectionTracker(30614): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):2)
W/ConnectionTracker(30614): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):3)
W/ConnectionTracker(30614): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(30614): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(30614): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):6)
W/DynamiteModule(30614): Local module descriptor class for com.google.android.gms.googlecertificates not found.
I/DynamiteModule(30614): Considering local module com.google.android.gms.googlecertificates:0 and remote module com.google.android.gms.googlecertificates:6
I/DynamiteModule(30614): Selected remote version of com.google.android.gms.googlecertificates, version >= 6
I/DynamiteLoaderV2Impl(30614): [71] Googlecertificates
W/ter_geolocatio(30614): ClassLoaderContext type mismatch. expected=PCL, found=DLC (PCL[] | DLC[];PCL[/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk*1782575538:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes2.dex*382790293:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes3.dex*184402630:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes4.dex*3697552760:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes5.dex*3148882451:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes6.dex*4130421832:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes7.dex*2561763281:/data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk!classes8.dex*3579848402]PCL[/system/framework/org.apache.http.legacy.jar*4222887692])
W/ter_geolocatio(30614): Found duplicate classes, falling back to extracting from APK : /data/user_de/0/com.google.android.gms/app_chimera/m/000001dc/GoogleCertificates.apk
W/ter_geolocatio(30614): NOTE: This wastes RAM and hurts startup performance.
W/ter_geolocatio(30614): Found duplicated class when checking oat files: 'Landroidx/annotation/Keep;' in /data/app/com.matthiaschan.flutter_geolocation-YwDUb2lByOR1j5fFIfFZbA==/base.apk and /data/user_de/0/com.google.android.gms/app_chimera/m/000001dc/GoogleCertificates.apk
W/ter_geolocatio(30614):
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
I/chatty (30614): uid=10445(com.matthiaschan.flutter_geolocation) androidmapsapi- identical 27 lines
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
I/chatty (30614): uid=10445(com.matthiaschan.flutter_geolocation) GoogleApiHandle identical 27 lines
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getObject(Ljava/lang/Object;J)Ljava/lang/Object; (greylist, linking, allowed)
W/ter_geolocatio(30614): Accessing hidden method Lsun/misc/Unsafe;->getInt(Ljava/lang/Object;J)I (greylist, linking, allowed)
W/ConnectionTracker(30614): Exception thrown while unbinding
W/ConnectionTracker(30614): java.lang.IllegalArgumentException: Service not registered: lp@df9eea1
W/ConnectionTracker(30614): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1766)
W/ConnectionTracker(30614): at android.app.ContextImpl.unbindService(ContextImpl.java:1810)
W/ConnectionTracker(30614): at android.content.ContextWrapper.unbindService(ContextWrapper.java:741)
W/ConnectionTracker(30614): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):1)
W/ConnectionTracker(30614): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):2)
W/ConnectionTracker(30614): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):10)
W/ConnectionTracker(30614): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):2)
W/ConnectionTracker(30614): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):3)
W/ConnectionTracker(30614): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(30614): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(30614): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@204217081@20.42.17 (120400-0):6)
V/NativeCrypto(30614): SSL shutdown failed: ssl=0x7ded8b8048: I/O error during system call, Broken pipe
W/ter_geolocatio(30614): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
这是我来自 firebase 的数据
【问题讨论】:
【参考方案1】:首先不能在 initState 中调用异步函数 (getCurrentLocation),因为它最初是同步的。最好在 onMapCreated 函数中调用它。
第二次尝试使用此代码将标记添加到标记列表中: 在 populateClient 中的“for”函数内部:
final marker = Marker(
markerId: MarkerId(doc.docs[i].data()['clientName']),
position: LatLng(doc.docs[i].data()['location'].latitude, doc.docs[i].data()['location'].longitude),
draggable: false,
);
allMarkers.add(marker);
第三次在“for”周围使用 setState。 最后需要调用onMapCreated中的函数。
我通常不使用 firebase,但我仍然认为这将有助于解决您的问题。
【讨论】:
setState 围绕你的“for”解决问题。非常感谢。但是,不知道如何将 getCurrentLocation 放在 onMapCreated 函数中,这样做会出错。 尝试在 onMapCreated 函数的 getCurrentLocation 前面使用 await。请注意,当您调用 polulateClients 函数两次时,标记列表将包含所有标记两次,因为您每次调用此函数时只需将标记添加到列表中。尝试在“for”函数前使用 allMarkers.clear()(因此在开始向其添加标记之前列表为空)或只调用该函数一次(我不想在 getCurrentLocation 函数中调用它,因为您可能会在其他地方使用此功能)。以上是关于颤动的谷歌地图无法显示来自firebase的标记的主要内容,如果未能解决你的问题,请参考以下文章