我想用顫振制作一個應用程式來獲取位置資料(緯度和經度)并將其顯示在地圖上,
我唯一的問題是我不知道如何將位置資料提供給谷歌地圖以顯示它,我是新手,我在網上搜索并沒有得到任何答案,如果有人可以指導我或有關如何將位置資料提供給谷歌地圖的資訊,我將不勝感激。
uj5u.com熱心網友回復:
在一個集合上設定一個onSnapshot- 型別的偵聽器,該集合會更新(無論您想要多久)您在前端檔案中的緯度和經度(Flutter)。
有用的提示可能是:
https://firebase.google.com/docs/firestore/query-data/listen#dart
final docRef = db.collection("locations").doc('User Docs);
docRef.snapshots().listen(
// On the line below you can do whatever you'd like with the return info "event.data()"
(event) => print("current data: ${event.data()}"),
one rror: (error) => print("Listen failed: $error"),
);
uj5u.com熱心網友回復:
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:permission_handler/permission_handler.dart';
///initializing
Set<Marker> markers = {};
LatLng? startLocation;
GoogleMapController? mapController;
bool loader = false;
///current location permission
determinePosition(pr) async {
bool serviceEnabled;
loader = true;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (serviceEnabled) {
getPlace();
}
}
///getting current location and setting start location
void getPlace() async {
Map<Permission, PermissionStatus> statuses = await [
Permission.location,
].request();
if (await Permission.locationWhenInUse.serviceStatus.isEnabled) {
print('no');
}
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
startLocation = LatLng(position.latitude, position.longitude);
addMarkers();
}
///adding markers to googlemap
addMarkers() async {
markers.add(
Marker(
markerId:MarkerId('SomeId'),
position: LatLng(
double.parse(homeData!.data!.items![i].latitude),
double.parse(homeData!.data!.items![i].longitude),
),
infoWindow: InfoWindow(
//popup info
title: '',
snippet: '',
),
),
);
}
loader = false;
setState((){});
}
///map widget
Widget getMap(){return loader? GoogleMap(
zoomGesturesEnabled: true,
initialCameraPosition: CameraPosition(
target: control.startLocation ??
LatLng(24.874594, 67.075709), //initial position
zoom: 30, //initial zoom level
),
markers: control.markers, //markers to show on map
mapType: MapType.normal, //map type
onMapCreated: (controller) {
//method called when map is created
setState(() {
control.mapController = controller;
});
},
):CircleProgressIndecator(),}
here is some work of how to show your current location on googlemap hope it will help you
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487417.html
