Skip to content

refactor: Code refactor in the Example project. #16

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
63 changes: 41 additions & 22 deletions Example/GoogleMapsPlatformCombine/ViewController.swift
Original file line number Diff line number Diff line change
@@ -24,36 +24,55 @@ class ViewController: UIViewController {
private var publisher: GMSMapViewPublisher!
private var subscriptions: Set<AnyCancellable> = []

private let sydneyLatitude = -33.86
private let sydneyLongitude = 151.20
private let sydneyLatitude: Double = -33.86
private let sydneyLongitude: Double = 151.20
private let markerTitle: String = "Sydney"
private let markerSnippet: String = "Australia"

override func viewDidLoad() {
super.viewDidLoad()
let mapView: GMSMapView = createGMSCameraPositionAndMapView()
setupGMSMapView(mapView: mapView)
createGMSMarkerInTheCenterOfTheMap(mapView: mapView)
startAndEndMapsCombinePublisher(mapView: mapView)
}

// Create a GMSCameraPosition that tells the map to display the
// coordinate -33.86,151.20 at zoom level 6.
let camera = GMSCameraPosition.camera(withLatitude: sydneyLatitude, longitude: sydneyLongitude, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: view.frame, camera: camera)
view.addSubview(mapView)

// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: sydneyLatitude, longitude: sydneyLongitude)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
/// Create a GMSCameraPosition that tells the map to display the coordinate -33.86,151.20 at zoom level 6.
/// - Returns: GMSMapView
private func createGMSCameraPositionAndMapView() -> GMSMapView {
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: sydneyLatitude, longitude: sydneyLongitude, zoom: 6.0)
let mapView: GMSMapView = GMSMapView.map(withFrame: view.frame, camera: camera)
return mapView
}

// [START maps_ios_combine_publisher_camera_change_position]
let publisher = GMSMapViewPublisher(mapView: mapView)
publisher.didChangeCameraPosition.sink { cameraPosition in
print("Camera position at \(cameraPosition.target)")
}
// [END maps_ios_combine_publisher_camera_change_position]
.store(in: &subscriptions)
/// Setup GMSMapView
/// - Parameter mapView: GMSMapView
private func setupGMSMapView(mapView: GMSMapView) {
view.addSubview(mapView)
}

self.publisher = publisher
/// Creates a marker in the center of the map.
/// - Parameter mapView: GMSMapView
private func createGMSMarkerInTheCenterOfTheMap(mapView: GMSMapView) {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: sydneyLatitude, longitude: sydneyLongitude)
marker.title = markerTitle
marker.snippet = markerSnippet
marker.map = mapView
}

/// Start and End Maps iOS combine publisher camera change position
/// - Parameter mapView: GMSMapView
private func startAndEndMapsCombinePublisher(mapView: GMSMapView) {
// [START maps_ios_combine_publisher_camera_change_position]
let publisher = GMSMapViewPublisher(mapView: mapView)
publisher.didChangeCameraPosition.sink { cameraPosition in
print("Camera position at \(cameraPosition.target)")
}
// [END maps_ios_combine_publisher_camera_change_position]
.store(in: &subscriptions)

self.publisher = publisher
}

private func fetchPlaceSample() {