I have following view
import 'dart:async';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import '../../Controllers/mapview_controller.dart';
class MapViewerPage extends StatelessWidget {
MapViewerPage({Key? key}) : super(key: key);
final MapViewController mapcontroller = MapViewController();
final Completer<GoogleMapController> _controller = Completer();
final CameraPosition _initialCameraPosition = const CameraPosition(
target: LatLng(28.527582, 77.0688971),
zoom: 16,
);
final BitmapDescriptor defaultIcon =
BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueRed);
final List<Marker> allMarkers = [];
@override
Widget build(BuildContext context) {
return GetBuilder<MapViewController>(
init: mapcontroller,
builder: (controller) {
return SizedBox(
width: double.infinity,
height: double.infinity,
child: Obx(() => GoogleMap(
myLocationButtonEnabled: true,
markers:Set<Marker>.of(allMarkers),
mapType: MapType.normal,
initialCameraPosition: _initialCameraPosition,
onMapCreated: (GoogleMapController gmcontroller) {
_controller.complete(gmcontroller);
gmcontroller.setMapStyle(controller.loadMapStyle());
},
)),
),
});
}
getMarkers() {
//get markers from controller
var realdata = mapcontroller.realtimeList;
realdata.asMap().forEach((key, value) {
var _marker = Marker(
consumeTapEvents: true,
markerId: MarkerId(key.toString()),
position: LatLng(
double.parse(value.latitude), double.parse(value.longitude)),
onTap: () {
//do something here
});
allMarkers.add(_marker);
});
}
}
And my controller looks like this.
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import '../../Models/model_realtime.dart';
import '../../Providers/common_api_provider.dart';
class MapViewController extends GetxController {
var isLoading = false.obs;
var realtimeList = <ModelRealtime>[].obs;
@override
void onInit() {
fetchRealtimeData();
super.onInit();
}
void fetchRealtimeData() async {
try {
isLoading(true);
var realtime = await CommonApiProvider.realTimeData();
if (realtime != null) {
realtimeList.value = realtime;
}
} catch (e) {
isLoading(false);
// ignore: avoid_print
print(e);
rethrow;
} finally {
isLoading(false);
}
}
loadMapStyle() async {
String style = await rootBundle.loadString("assets/map_style.json");
return style;
}
}
These codes work pretty well, but it seems to reload/refresh Googlemap everytime new data arrives. How do I update markers' position only everytime the new latitude, longitude updated in the database without reloading/refreshing Googlemap?
Thanks
Obx( () -> GoogleMap (...
refreshes/reloads Googlemap everytime new marker data comes? – Lancaster