Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markers aren't updating when items changed #7

Open
berkeenesakt opened this issue Aug 3, 2023 · 0 comments
Open

Markers aren't updating when items changed #7

berkeenesakt opened this issue Aug 3, 2023 · 0 comments

Comments

@berkeenesakt
Copy link

berkeenesakt commented Aug 3, 2023

I write a code that only shows items on visible region. But the markers are not showing on map even it shows on bottom tile after moving the map and changing the visible region.

My code:

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:google_maps_utils/utils/spherical_utils.dart';
import 'package:interactive_maps_marker/interactive_maps_marker.dart';

import 'widgets/bottom_tile.dart';

class LazyLoadingUsage extends StatefulWidget {
  @override
  State<LazyLoadingUsage> createState() => _LazyLoadingUsageState();
}

class _LazyLoadingUsageState extends State<LazyLoadingUsage> {
  List<MarkerItem> markers = []
    ..add(MarkerItem(id: 1, latitude: 31.4673274, longitude: 74.2637687))
    ..add(MarkerItem(id: 2, latitude: 31.4718461, longitude: 74.3531591))
    ..add(MarkerItem(id: 3, latitude: 31.5325107, longitude: 74.3610325))
    ..add(MarkerItem(id: 4, latitude: 31.4668809, longitude: 74.31354));

  List<MarkerItem> markersShown = []
    ..add(MarkerItem(id: 1, latitude: 31.4673274, longitude: 74.2637687))
    ..add(MarkerItem(id: 2, latitude: 31.4718461, longitude: 74.3531591))
    ..add(MarkerItem(id: 3, latitude: 31.5325107, longitude: 74.3610325))
    ..add(MarkerItem(id: 4, latitude: 31.4668809, longitude: 74.31354));

  CameraPosition? cameraPosition;

  final InteractiveMapsController mapController = InteractiveMapsController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Lazy Loading Usage'),
      ),
      body: InteractiveMapsMarker(
        controller: mapController,
        items: markersShown,
        onCameraMove: (p0) async {
          final bounds =
              await mapController.getMapController()!.getVisibleRegion();
          LatLng northeast = bounds.northeast;
          LatLng southwest = bounds.southwest;
          Point center = Point((northeast.latitude + southwest.latitude) / 2,
              (northeast.longitude + southwest.longitude) / 2);
          Point northEastPoint = Point(northeast.latitude, northeast.longitude);
          //calculate the radius of visible region
          final radius =
              SphericalUtils.computeDistanceBetween(center, northEastPoint);
          //filter the markers which are visible in the visible region
          markersShown = markers
              .where((element) =>
                  SphericalUtils.computeDistanceBetween(
                      center, Point(element.latitude, element.longitude)) <
                  radius)
              .toList();
          setState(() {});
        },
        center: LatLng(31.4906504, 74.319872),
        itemContent: (context, index) {
          MarkerItem item = markersShown[index];
          return BottomTile(item: item);
        },
      ),
    );
  }
}
trim.A8EE8321-4FA7-406D-8509-DA25A8E4E4F2.MOV
@berkeenesakt berkeenesakt changed the title Markers aren't updating when items changed. Markers aren't updating when items changed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant