Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: example
description: A new Flutter project.

publish_to: 'none'
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
Expand All @@ -20,8 +20,8 @@ dependencies:
flutter:
sdk: flutter

google_maps_flutter: ^2.5.2
google_maps_flutter_web: ^0.5.4+3
google_maps_flutter: ^2.6.0
google_maps_flutter_web: ^0.5.6+2
google_maps_cluster_manager:
path: ../

Expand Down
5 changes: 0 additions & 5 deletions example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
// gestures. You can also use WidgetTester to find child widgets in the widget
// tree, read text, and verify that the values of widget properties are correct.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:example/main.dart';

void main() {
// testWidgets('Counter increments smoke test', (WidgetTester tester) async {
// // Build our app and trigger a frame.
Expand Down
5 changes: 3 additions & 2 deletions lib/src/cluster_item.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'
as gMFPI;

mixin ClusterItem {
LatLng get location;
gMFPI.LatLng get location;

String? _geohash;
String get geohash => _geohash ??=
Expand Down
41 changes: 22 additions & 19 deletions lib/src/cluster_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:google_maps_cluster_manager/google_maps_cluster_manager.dart';
import 'package:google_maps_cluster_manager/src/max_dist_clustering.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart';
import 'package:google_maps_flutter_platform_interface/google_maps_flutter_platform_interface.dart'
as gMFPI;

enum ClusterAlgorithm { GEOHASH, MAX_DIST }

Expand All @@ -17,7 +18,7 @@ class MaxDistParams {

class ClusterManager<T extends ClusterItem> {
ClusterManager(this._items, this.updateMarkers,
{Future<Marker> Function(Cluster<T>)? markerBuilder,
{Future<gMFPI.Marker> Function(Cluster<T>)? markerBuilder,
this.levels = const [1, 4.25, 6.75, 8.25, 11.5, 14.5, 16.0, 16.5, 20.0],
this.extraPercent = 0.5,
this.maxItemsForMaxDistAlgo = 200,
Expand All @@ -28,13 +29,13 @@ class ClusterManager<T extends ClusterItem> {
assert(levels.length <= precision);

/// Method to build markers
final Future<Marker> Function(Cluster<T>) markerBuilder;
final Future<gMFPI.Marker> Function(Cluster<T>) markerBuilder;

// Num of Items to switch from MAX_DIST algo to GEOHASH
final int maxItemsForMaxDistAlgo;

/// Function to update Markers on Google Map
final void Function(Set<Marker>) updateMarkers;
final void Function(Set<gMFPI.Marker>) updateMarkers;

/// Zoom levels configuration
final List<double> levels;
Expand Down Expand Up @@ -68,7 +69,8 @@ class ClusterManager<T extends ClusterItem> {
/// Set Google Map Id for the cluster manager
void setMapId(int mapId, {bool withUpdate = true}) async {
_mapId = mapId;
_zoom = await GoogleMapsFlutterPlatform.instance.getZoomLevel(mapId: mapId);
_zoom = await gMFPI.GoogleMapsFlutterPlatform.instance
.getZoomLevel(mapId: mapId);
if (withUpdate) updateMap();
}

Expand All @@ -80,7 +82,7 @@ class ClusterManager<T extends ClusterItem> {
void _updateClusters() async {
List<Cluster<T>> mapMarkers = await getMarkers();

final Set<Marker> markers =
final Set<gMFPI.Marker> markers =
Set.from(await Future.wait(mapMarkers.map((m) => markerBuilder(m))));

updateMarkers(markers);
Expand All @@ -99,7 +101,7 @@ class ClusterManager<T extends ClusterItem> {
}

/// Method called on camera move
void onCameraMove(CameraPosition position, {forceUpdate = false}) {
void onCameraMove(gMFPI.CameraPosition position, {forceUpdate = false}) {
_zoom = position.zoom;
if (forceUpdate) {
updateMap();
Expand All @@ -110,10 +112,11 @@ class ClusterManager<T extends ClusterItem> {
Future<List<Cluster<T>>> getMarkers() async {
if (_mapId == null) return List.empty();

final LatLngBounds mapBounds = await GoogleMapsFlutterPlatform.instance
final gMFPI.LatLngBounds mapBounds = await gMFPI
.GoogleMapsFlutterPlatform.instance
.getVisibleRegion(mapId: _mapId!);

late LatLngBounds inflatedBounds;
late gMFPI.LatLngBounds inflatedBounds;
if (clusterAlgorithm == ClusterAlgorithm.GEOHASH) {
inflatedBounds = _inflateBounds(mapBounds);
} else {
Expand Down Expand Up @@ -141,7 +144,7 @@ class ClusterManager<T extends ClusterItem> {
return markers;
}

LatLngBounds _inflateBounds(LatLngBounds bounds) {
gMFPI.LatLngBounds _inflateBounds(gMFPI.LatLngBounds bounds) {
// Bounds that cross the date line expand compared to their difference with the date line
double lng = 0;
if (bounds.northeast.longitude < bounds.southwest.longitude) {
Expand All @@ -160,10 +163,10 @@ class ClusterManager<T extends ClusterItem> {
double eLng = (bounds.northeast.longitude + lng).clamp(-_maxLng, _maxLng);
double wLng = (bounds.southwest.longitude - lng).clamp(-_maxLng, _maxLng);

return LatLngBounds(
southwest: LatLng(bounds.southwest.latitude - lat, wLng),
northeast:
LatLng(bounds.northeast.latitude + lat, lng != 0 ? eLng : _maxLng),
return gMFPI.LatLngBounds(
southwest: gMFPI.LatLng(bounds.southwest.latitude - lat, wLng),
northeast: gMFPI.LatLng(
bounds.northeast.latitude + lat, lng != 0 ? eLng : _maxLng),
);
}

Expand Down Expand Up @@ -214,10 +217,10 @@ class ClusterManager<T extends ClusterItem> {
return _computeClusters(newInputList, markerItems, level: level);
}

static Future<Marker> Function(Cluster) get _basicMarkerBuilder =>
static Future<gMFPI.Marker> Function(Cluster) get _basicMarkerBuilder =>
(cluster) async {
return Marker(
markerId: MarkerId(cluster.getId()),
return gMFPI.Marker(
markerId: gMFPI.MarkerId(cluster.getId()),
position: cluster.location,
onTap: () {
print(cluster);
Expand All @@ -227,7 +230,7 @@ class ClusterManager<T extends ClusterItem> {
);
};

static Future<BitmapDescriptor> _getBasicClusterBitmap(int size,
static Future<gMFPI.BitmapDescriptor> _getBasicClusterBitmap(int size,
{String? text}) async {
final PictureRecorder pictureRecorder = PictureRecorder();
final Canvas canvas = Canvas(pictureRecorder);
Expand All @@ -254,6 +257,6 @@ class ClusterManager<T extends ClusterItem> {
final img = await pictureRecorder.endRecording().toImage(size, size);
final data = await img.toByteData(format: ImageByteFormat.png) as ByteData;

return BitmapDescriptor.fromBytes(data.buffer.asUint8List());
return gMFPI.BitmapDescriptor.fromBytes(data.buffer.asUint8List());
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter

google_maps_flutter_platform_interface: ^2.4.3
google_maps_flutter_platform_interface: ^2.6.0

dev_dependencies:
flutter_test:
Expand Down