This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[google_maps_flutter] Marker clustering support #6752
Closed
jokerttu
wants to merge
7
commits into
flutter:main
from
CodemateLtd:google_maps_flutter_cluster_support
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d08e6ef
[google_maps_flutter] Google maps flutter marker clustering support
jokerttu a8efc71
[google_maps_flutter] Revert copyWithDefaults method from marker class
jokerttu 35b2f4a
[google_maps_flutter] Google maps flutter cluster support improvement…
TimoPieti dadf697
[google_maps_flutter] Refactored syntax
TimoPieti a886b6e
[google_maps_flutter_android] Remove unnecessary comments
jokerttu 3faff92
[google_maps_flutter] Fix formatter issues
TimoPieti 0f67dd1
[google_maps_flutter] Add missing license block
jokerttu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
237 changes: 237 additions & 0 deletions
237
packages/google_maps_flutter/google_maps_flutter/example/lib/clustering.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,237 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
// ignore_for_file: public_member_api_docs | ||
|
||
import 'dart:math'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:google_maps_flutter/google_maps_flutter.dart'; | ||
|
||
import 'page.dart'; | ||
|
||
class ClusteringPage extends GoogleMapExampleAppPage { | ||
const ClusteringPage({Key? key}) | ||
: super(const Icon(Icons.place), 'Manage clustering', key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const ClusteringBody(); | ||
} | ||
} | ||
|
||
class ClusteringBody extends StatefulWidget { | ||
const ClusteringBody({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<StatefulWidget> createState() => ClusteringBodyState(); | ||
} | ||
|
||
typedef MarkerUpdateAction = Marker Function(Marker marker); | ||
|
||
class ClusteringBodyState extends State<ClusteringBody> { | ||
ClusteringBodyState(); | ||
static const LatLng center = LatLng(-33.86711, 151.1547171); | ||
static const double _scaleFactor = 0.05; | ||
|
||
GoogleMapController? controller; | ||
Map<ClusterManagerId, ClusterManager> clusterManagers = | ||
<ClusterManagerId, ClusterManager>{}; | ||
Map<MarkerId, Marker> markers = <MarkerId, Marker>{}; | ||
MarkerId? selectedMarker; | ||
int _clusterManagerIdCounter = 1; | ||
int _markerIdCounter = 1; | ||
Cluster? lastCluster; | ||
|
||
// ignore: use_setters_to_change_properties | ||
void _onMapCreated(GoogleMapController controller) { | ||
this.controller = controller; | ||
} | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
} | ||
|
||
void _onMarkerTapped(MarkerId markerId) { | ||
final Marker? tappedMarker = markers[markerId]; | ||
if (tappedMarker != null) { | ||
setState(() { | ||
final MarkerId? previousMarkerId = selectedMarker; | ||
if (previousMarkerId != null && markers.containsKey(previousMarkerId)) { | ||
final Marker resetOld = markers[previousMarkerId]! | ||
.copyWith(iconParam: BitmapDescriptor.defaultMarker); | ||
markers[previousMarkerId] = resetOld; | ||
} | ||
selectedMarker = markerId; | ||
final Marker newMarker = tappedMarker.copyWith( | ||
iconParam: BitmapDescriptor.defaultMarkerWithHue( | ||
BitmapDescriptor.hueGreen, | ||
), | ||
); | ||
markers[markerId] = newMarker; | ||
}); | ||
} | ||
} | ||
|
||
void _addClusterManager() { | ||
final int clusterManagerCount = clusterManagers.length; | ||
|
||
if (clusterManagerCount == 3) { | ||
return; | ||
} | ||
|
||
final String clusterManagerIdVal = | ||
'cluster_manager_id_$_clusterManagerIdCounter'; | ||
_clusterManagerIdCounter++; | ||
final ClusterManagerId clusterManagerId = | ||
ClusterManagerId(clusterManagerIdVal); | ||
|
||
final ClusterManager clusterManager = ClusterManager( | ||
clusterManagerId: clusterManagerId, | ||
onClusterTap: (Cluster cluster) => setState(() { | ||
lastCluster = cluster; | ||
}), | ||
); | ||
|
||
setState(() { | ||
clusterManagers[clusterManagerId] = clusterManager; | ||
}); | ||
_addMarkersToCluster(clusterManager); | ||
} | ||
|
||
void _removeClusterManager(ClusterManager clusterManager) { | ||
setState(() { | ||
// Remove markers managed by cluster manager to be removed. | ||
markers.removeWhere((MarkerId key, Marker marker) => | ||
marker.clusterManagerId == clusterManager.clusterManagerId); | ||
// Remove cluster manager. | ||
clusterManagers.remove(clusterManager.clusterManagerId); | ||
}); | ||
} | ||
|
||
void _addMarkersToCluster(ClusterManager clusterManager) { | ||
for (int i = 0; i < 15; i++) { | ||
reidbaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final String markerIdVal = | ||
'${clusterManager.clusterManagerId.value}_marker_id_$_markerIdCounter'; | ||
_markerIdCounter++; | ||
final MarkerId markerId = MarkerId(markerIdVal); | ||
|
||
final int clusterManagerIndex = | ||
clusterManagers.values.toList().indexOf(clusterManager); | ||
final Marker marker = Marker( | ||
clusterManagerId: clusterManager.clusterManagerId, | ||
markerId: markerId, | ||
position: LatLng( | ||
center.latitude + _getRandomOffset(), | ||
center.longitude + | ||
_getRandomOffset() + | ||
clusterManagerIndex * _scaleFactor * 2, | ||
), | ||
infoWindow: InfoWindow(title: markerIdVal, snippet: '*'), | ||
onTap: () => _onMarkerTapped(markerId), | ||
); | ||
markers[markerId] = marker; | ||
} | ||
setState(() {}); | ||
} | ||
|
||
double _getRandomOffset() { | ||
return (Random().nextDouble() - 0.5) * _scaleFactor; | ||
} | ||
|
||
void _remove(MarkerId markerId) { | ||
setState(() { | ||
if (markers.containsKey(markerId)) { | ||
markers.remove(markerId); | ||
} | ||
}); | ||
} | ||
|
||
void _changeMarkersRotation() { | ||
for (final MarkerId markerId in markers.keys) { | ||
final Marker marker = markers[markerId]!; | ||
final double current = marker.rotation; | ||
markers[markerId] = marker.copyWith( | ||
rotationParam: current == 315.0 ? 0.0 : current + 45.0, | ||
); | ||
} | ||
setState(() {}); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final MarkerId? selectedId = selectedMarker; | ||
return Stack(children: <Widget>[ | ||
Column( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: <Widget>[ | ||
Expanded( | ||
child: GoogleMap( | ||
onMapCreated: _onMapCreated, | ||
initialCameraPosition: const CameraPosition( | ||
target: LatLng(-33.852, 151.211), | ||
jokerttu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
zoom: 11.0, | ||
), | ||
markers: Set<Marker>.of(markers.values), | ||
clusterManagers: Set<ClusterManager>.of(clusterManagers.values), | ||
), | ||
), | ||
Row( | ||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, | ||
children: <Widget>[ | ||
TextButton( | ||
onPressed: () => _addClusterManager(), | ||
child: const Text('Add cluster manager'), | ||
reidbaker marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
TextButton( | ||
onPressed: clusterManagers.isEmpty | ||
? null | ||
: () => _removeClusterManager(clusterManagers.values.last), | ||
child: const Text('Remove cluster manager'), | ||
), | ||
], | ||
), | ||
Wrap( | ||
alignment: WrapAlignment.spaceEvenly, | ||
children: <Widget>[ | ||
for (final MapEntry<ClusterManagerId, ClusterManager> clusterEntry | ||
in clusterManagers.entries) | ||
TextButton( | ||
onPressed: () => _addMarkersToCluster(clusterEntry.value), | ||
child: Text('Add markers to ${clusterEntry.key.value}'), | ||
), | ||
], | ||
), | ||
Wrap( | ||
alignment: WrapAlignment.spaceEvenly, | ||
children: <Widget>[ | ||
TextButton( | ||
onPressed: selectedId == null | ||
? null | ||
: () { | ||
_remove(selectedId); | ||
setState(() { | ||
selectedMarker = null; | ||
}); | ||
}, | ||
child: const Text('Remove selected marker'), | ||
), | ||
TextButton( | ||
onPressed: | ||
markers.isEmpty ? null : () => _changeMarkersRotation(), | ||
child: const Text('Change all markers rotation'), | ||
), | ||
], | ||
), | ||
if (lastCluster != null) | ||
Padding( | ||
padding: const EdgeInsets.all(10), | ||
child: Text( | ||
'Cluster with ${lastCluster!.count} markers clicked at ${lastCluster!.position}')), | ||
], | ||
), | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.