Skip to content

Commit

Permalink
feat: Enhanced delegate sample with multiple callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewbishop committed Dec 1, 2024
1 parent c5d363f commit 1951ed8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
4 changes: 2 additions & 2 deletions GoogleMaps-SwiftUI/GoogleMaps-SwiftUI/Samples/BasicMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ struct BasicMap: View {

@State private var mapOptions: GMSMapViewOptions = {
var options = GMSMapViewOptions()
// Initialize map centered on San Francisco
options.camera = .camera(.sanFrancisco)
// Initialize map centered at Google HQ
options.camera = .camera(.googleplex)

// Or with custom zoom level for closer view
// options.camera = .camera(.sanFrancisco, zoom: 15)
Expand Down
47 changes: 37 additions & 10 deletions GoogleMaps-SwiftUI/GoogleMaps-SwiftUI/Samples/MapWithDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,53 @@ import GoogleMaps

struct MapWithDelegate: View {

@State var response: String = ""
@State private var mapOptions: GMSMapViewOptions = {
var options = GMSMapViewOptions()
// Initialize map centered on San Francisco
options.camera = .camera(.googleplex)
options.camera = .camera(.sanFrancisco)

// Or with custom zoom level for closer view
// options.camera = .camera(.sanFrancisco, zoom: 15)
return options
}()

// multiple marker example
let multipleMarkers = [
GMSMarker(position: .chinatownGate),
GMSMarker(position: .coitTower),
GMSMarker(position: .ferryBuilding),
GMSMarker(position: .fishermansWharf)
]

var body: some View {

/// Tap handling is implemented through a delegate pattern: GoogleMapView exposes an onMapTapped modifier
/// that stores a coordinate handler in GoogleMapViewDelegate. When the underlying GMSMapView detects a tap,
/// it calls the delegate's mapView(_:didTapAt:) method, which then executes the stored handler with the
/// tap coordinates.
GoogleMapView(options: $mapOptions)
.onMapTapped { coordinate in
print("Map tapped at: \(coordinate.latitude), \(coordinate.longitude)")

VStack(spacing: 16) {
GoogleMapView(options: $mapOptions)
.mapMarkers(multipleMarkers)
.onMarkerTapped { marker in
response = "Marker tapped at: \(marker.position)"
return true
}
.onMapTapped { coordinate in
response = "Map tapped at: \(coordinate.latitude), \(coordinate.longitude)"
}
.ignoresSafeAreaExceptTop()
.frame(maxWidth: .infinity, minHeight: 325)

HStack {
VStack(alignment: .leading, spacing: 8) {
Text("Tap a Map location or Marker")
.font(.headline)

Text(response)
.font(.body)
.foregroundColor(.secondary)
}
Spacer()
}
.ignoresSafeAreaExceptTop() //optional property for samples display
.padding(.horizontal)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ struct MapWithMarker: View {
GoogleMapView(options: $mapOptions)
//Adds one or more markers to be displayed on the map
.mapMarkers(singleMarker)

.onMarkerTapped { marker in
print("Marker tapped at: \(marker.position)")
return true
}
.onMapTapped { coordinate in
print("Map tapped at: \(coordinate.latitude), \(coordinate.longitude)")
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct MapWithMarkers: View {
}()

// multiple marker example
let multipleMarker = [
let multipleMarkers = [
GMSMarker(position: .chinatownGate),
GMSMarker(position: .coitTower),
GMSMarker(position: .ferryBuilding),
Expand All @@ -37,7 +37,7 @@ struct MapWithMarkers: View {
var body: some View {
GoogleMapView(options: $mapOptions)
//Adds one or more markers to be displayed on the map
.mapMarkers(multipleMarker)
.mapMarkers(multipleMarkers)
.ignoresSafeAreaExceptTop() //optional property for samples display
}
}
Expand Down

0 comments on commit 1951ed8

Please sign in to comment.