Skip to content

Commit da2e1a6

Browse files
[google_maps_flutter] Convert marker controllers to Swift (flutter#12662)
This converts MarkerController and ClusterManagersController to Swift in the `_sdk*` packages. The remaining Obj-C code will be migrated in a series of follow-up PRs to keep the scope of each PR reasonable for review. The conversion process was: - Initial conversion via Gemini, with explicit instruction to keep the structure the same. - Side-by-side manual review of the old and new versions of the files. - Manual fixes and improvements. Part of flutter#119108 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent c8faec1 commit da2e1a6

58 files changed

Lines changed: 1543 additions & 2249 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.18.10
2+
3+
* Converts marker controllers to Swift.
4+
15
## 2.18.9
26

37
* Updates pigeon dev_dependency to ^27.3.2 for analyzer 14 compatibility.

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/ClusterManagersControllerTests.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ import Testing
2020
let mapView = PartiallyMockedMapView(options: mapViewOptions)
2121
let eventHandler = TestMapEventHandler()
2222

23-
let clusterManagersController = FGMClusterManagersController(
23+
let clusterManagersController = ClusterManagersController(
2424
mapView: mapView,
2525
eventDelegate: eventHandler
2626
)
2727

28-
let markersController = FGMMarkersController(
28+
let markersController = MarkersController(
2929
mapView: mapView,
3030
eventDelegate: eventHandler,
3131
clusterManagersController: clusterManagersController,

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/MarkerControllerTests.swift‎

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import Testing
2323
func markersController(
2424
withMapView mapView: GMSMapView,
2525
eventDelegate: NSObject & FGMMapEventDelegate
26-
) -> FGMMarkersController {
27-
return FGMMarkersController(
26+
) -> MarkersController {
27+
return MarkersController(
2828
mapView: mapView,
2929
eventDelegate: eventDelegate,
3030
clusterManagersController: nil,
@@ -73,10 +73,8 @@ import Testing
7373
)
7474
])
7575

76-
let markerController = try #require(
77-
controller.markerIdentifierToController[markerIdentifier] as? FGMMarkerController
78-
)
79-
let marker = try #require(markerController.marker)
76+
let markerController = try #require(controller.markerIdentifierToController[markerIdentifier])
77+
let marker = markerController.marker
8078

8179
let delta = 0.0001
8280
#expect(abs(Double(marker.opacity) - alpha) <= delta)
@@ -117,10 +115,8 @@ import Testing
117115
)
118116
])
119117

120-
let markerController = try #require(
121-
controller.markerIdentifierToController[markerIdentifier] as? FGMMarkerController
122-
)
123-
let marker = try #require(markerController.marker)
118+
let markerController = try #require(controller.markerIdentifierToController[markerIdentifier])
119+
let marker = markerController.marker
124120

125121
#expect(marker.isDraggable)
126122
}
@@ -156,10 +152,8 @@ import Testing
156152
)
157153
])
158154

159-
let markerController = try #require(
160-
controller.markerIdentifierToController[markerIdentifier] as? FGMMarkerController
161-
)
162-
let marker = try #require(markerController.marker)
155+
let markerController = try #require(controller.markerIdentifierToController[markerIdentifier])
156+
let marker = markerController.marker
163157

164158
#expect(marker.isFlat)
165159
}
@@ -195,10 +189,8 @@ import Testing
195189
)
196190
])
197191

198-
let markerController = try #require(
199-
controller.markerIdentifierToController[markerIdentifier] as? FGMMarkerController
200-
)
201-
let marker = try #require(markerController.marker)
192+
let markerController = try #require(controller.markerIdentifierToController[markerIdentifier])
193+
let marker = markerController.marker
202194

203195
// Visibility is controlled by being set to a map.
204196
#expect(marker.map != nil)
@@ -237,10 +229,8 @@ import Testing
237229
)
238230
])
239231

240-
let markerController = try #require(
241-
controller.markerIdentifierToController[markerIdentifier] as? FGMMarkerController
242-
)
243-
let marker = try #require(markerController.marker)
232+
let markerController = try #require(controller.markerIdentifierToController[markerIdentifier])
233+
let marker = markerController.marker
244234

245235
let delta = 0.0001
246236
#expect(abs(Double(marker.infoWindowAnchor.x) - anchorX) <= delta)
@@ -254,7 +244,7 @@ import Testing
254244
let collisionBehavior = FGMPlatformMarkerCollisionBehaviorBox(
255245
value: .requiredAndHidesOptional
256246
)
257-
FGMMarkerController.update(
247+
MarkerController.update(
258248
marker,
259249
from: FGMPlatformMarker.make(
260250
withAlpha: 1.0,
@@ -276,7 +266,7 @@ import Testing
276266
clusterManagerId: nil,
277267
collisionBehavior: collisionBehavior
278268
),
279-
with: MarkerControllerTests.mapView(),
269+
mapView: MarkerControllerTests.mapView(),
280270
assetProvider: TestAssetProvider(),
281271
screenScale: 1,
282272
usingOpacityForVisibility: false
@@ -285,13 +275,13 @@ import Testing
285275
}
286276

287277
@Test func assetProviderIsRetained() {
288-
var markerController: FGMMarkersController?
278+
var markerController: MarkersController?
289279
weak var weakAssetProvider: TestAssetProvider?
290280
autoreleasepool {
291281
let assetProvider = TestAssetProvider()
292282
weakAssetProvider = assetProvider
293283

294-
markerController = FGMMarkersController(
284+
markerController = MarkersController(
295285
mapView: MarkerControllerTests.mapView(),
296286
eventDelegate: TestMapEventHandler(),
297287
clusterManagersController: nil,

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/example/ios/RunnerTests/RunnerTests-Bridging-Header.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44

55
// Import private _Test.h headers from the plugin framework
66
#import <google_maps_flutter_ios_sdk10_objc/FGMHeatmapController_Test.h>
7-
#import <google_maps_flutter_ios_sdk10_objc/FGMMarkerController_Test.h>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import Flutter
6+
import GoogleMaps
7+
8+
#if canImport(google_maps_flutter_ios_sdk10_objc)
9+
import google_maps_flutter_ios_sdk10_objc
10+
#endif
11+
12+
/// A controller that manages all of the cluster managers on a map.
13+
class ClusterManagersController: NSObject {
14+
private var clusterManagerIdentifierToManagers: [String: GMUClusterManager] = [:]
15+
private weak var eventDelegate: FGMMapEventDelegate?
16+
private weak var mapView: GMSMapView?
17+
18+
init(mapView: GMSMapView, eventDelegate: FGMMapEventDelegate) {
19+
self.mapView = mapView
20+
self.eventDelegate = eventDelegate
21+
super.init()
22+
}
23+
24+
func add(_ clusterManagersToAdd: [FGMPlatformClusterManager]) {
25+
for clusterManager in clusterManagersToAdd {
26+
addClusterManager(clusterManager.identifier)
27+
}
28+
}
29+
30+
func addClusterManager(_ identifier: String) {
31+
guard let mapView = mapView else { return }
32+
let algorithm = GMUNonHierarchicalDistanceBasedAlgorithm()
33+
let iconGenerator = GMUDefaultClusterIconGenerator()
34+
let renderer = GMUDefaultClusterRenderer(mapView: mapView, clusterIconGenerator: iconGenerator)
35+
clusterManagerIdentifierToManagers[identifier] = GMUClusterManager(
36+
map: mapView,
37+
algorithm: algorithm,
38+
renderer: renderer
39+
)
40+
}
41+
42+
func removeClusterManagers(withIdentifiers identifiers: [String]) {
43+
for identifier in identifiers {
44+
if let clusterManager = clusterManagerIdentifierToManagers[identifier] {
45+
clusterManager.clearItems()
46+
clusterManagerIdentifierToManagers.removeValue(forKey: identifier)
47+
}
48+
}
49+
}
50+
51+
func clusterManager(withIdentifier identifier: String) -> GMUClusterManager? {
52+
return clusterManagerIdentifierToManagers[identifier]
53+
}
54+
55+
func invokeClusteringForEachClusterManager() {
56+
clusterManagerIdentifierToManagers.values.forEach({ $0.cluster() })
57+
}
58+
59+
func clusters(
60+
withIdentifier identifier: String,
61+
error: AutoreleasingUnsafeMutablePointer<FlutterError?>
62+
) -> [FGMPlatformCluster]? {
63+
guard let clusterManager = clusterManagerIdentifierToManagers[identifier] else {
64+
error.pointee = FlutterError(
65+
code: "Invalid clusterManagerId",
66+
message: "getClusters called with invalid clusterManagerId",
67+
details: "clusterManagerId was: '\(identifier)'"
68+
)
69+
return nil
70+
}
71+
guard let mapView = mapView else { return [] }
72+
73+
// Ref:
74+
// https://github.com/googlemaps/google-maps-ios-utils/blob/0e7ed81f1bbd9d29e4529c40ae39b0791b0a0eb8/src/Clustering/GMUClusterManager.m#L94.
75+
let integralZoom = floorf(Float(mapView.camera.zoom) + 0.5)
76+
let clusters = clusterManager.algorithm.clusters(atZoom: integralZoom)
77+
return clusters.map { pigeonCluster(for: $0, clusterManagerIdentifier: identifier) }
78+
}
79+
80+
func didTap(_ cluster: GMUStaticCluster) {
81+
guard let clusterManagerId = clusterManagerIdentifier(for: cluster) else { return }
82+
let platformCluster = pigeonCluster(for: cluster, clusterManagerIdentifier: clusterManagerId)
83+
eventDelegate?.didTap(platformCluster)
84+
}
85+
86+
/// Returns the cluster manager identifier for given cluster.
87+
private func clusterManagerIdentifier(for cluster: GMUStaticCluster) -> String? {
88+
if let firstMarker = cluster.items.first as? GMSMarker {
89+
return clusterManagerIdentifierFromMarker(firstMarker)
90+
}
91+
return nil
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#if canImport(google_maps_flutter_ios_sdk10_objc)
6+
import google_maps_flutter_ios_sdk10_objc
7+
#endif
8+
9+
/// Converts a GMUCluster to its Pigeon representation.
10+
func pigeonCluster(
11+
for cluster: GMUCluster,
12+
clusterManagerIdentifier: String
13+
) -> FGMPlatformCluster {
14+
var bounds = GMSCoordinateBounds()
15+
for item in cluster.items {
16+
bounds = bounds.includingCoordinate(item.position)
17+
}
18+
19+
let markerIds = cluster.items.filter { $0 is GMSMarker }.compactMap {
20+
markerIdentifierFromMarker($0 as! GMSMarker)
21+
}
22+
23+
return FGMPlatformCluster.make(
24+
withClusterManagerId: clusterManagerIdentifier,
25+
position: FGMGetPigeonLatLngForCoordinate(cluster.position),
26+
bounds: FGMGetPigeonLatLngBoundsForCoordinateBounds(bounds),
27+
markerIds: markerIds
28+
)
29+
}

‎packages/google_maps_flutter/google_maps_flutter_ios_sdk10/ios/google_maps_flutter_ios_sdk10/Sources/google_maps_flutter_ios_sdk10/GoogleMapController.swift‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
122122
var trackCameraPosition = false
123123

124124
/// Sub-controllers for managing individual map features.
125-
let clusterManagersController: FGMClusterManagersController
126-
let markersController: FGMMarkersController
125+
let clusterManagersController: ClusterManagersController
126+
let markersController: MarkersController
127127
let polygonsController: PolygonsController
128128
let polylinesController: PolylinesController
129129
let circlesController: CirclesController
@@ -198,11 +198,11 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
198198

199199
let markerType = creationParameters.mapConfiguration.markerType
200200

201-
clusterManagersController = FGMClusterManagersController(
201+
clusterManagersController = ClusterManagersController(
202202
mapView: mapView,
203203
eventDelegate: mapEventHandler
204204
)
205-
markersController = FGMMarkersController(
205+
markersController = MarkersController(
206206
mapView: mapView,
207207
eventDelegate: mapEventHandler,
208208
clusterManagersController: clusterManagersController,
@@ -374,32 +374,32 @@ public class GoogleMapController: NSObject, GMSMapViewDelegate, FlutterPlatformV
374374
clusterManagersController.didTap(cluster)
375375
return false
376376
}
377-
if let markerId = FGMGetMarkerIdentifierFromMarker(marker) {
377+
if let markerId = markerIdentifierFromMarker(marker) {
378378
return markersController.didTapMarker(withIdentifier: markerId)
379379
}
380380
return false
381381
}
382382

383383
public func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
384-
if let markerId = FGMGetMarkerIdentifierFromMarker(marker) {
384+
if let markerId = markerIdentifierFromMarker(marker) {
385385
markersController.didEndDraggingMarker(withIdentifier: markerId, location: marker.position)
386386
}
387387
}
388388

389389
public func mapView(_ mapView: GMSMapView, didBeginDragging marker: GMSMarker) {
390-
if let markerId = FGMGetMarkerIdentifierFromMarker(marker) {
390+
if let markerId = markerIdentifierFromMarker(marker) {
391391
markersController.didStartDraggingMarker(withIdentifier: markerId, location: marker.position)
392392
}
393393
}
394394

395395
public func mapView(_ mapView: GMSMapView, didDrag marker: GMSMarker) {
396-
if let markerId = FGMGetMarkerIdentifierFromMarker(marker) {
396+
if let markerId = markerIdentifierFromMarker(marker) {
397397
markersController.didDragMarker(withIdentifier: markerId, location: marker.position)
398398
}
399399
}
400400

401401
public func mapView(_ mapView: GMSMapView, didTapInfoWindowOf marker: GMSMarker) {
402-
if let markerId = FGMGetMarkerIdentifierFromMarker(marker) {
402+
if let markerId = markerIdentifierFromMarker(marker) {
403403
markersController.didTapInfoWindowOfMarker(withIdentifier: markerId)
404404
}
405405
}

0 commit comments

Comments
 (0)