I am utilizing the iOS Google Maps SDK to point out maps in my app.
On my maps, I additionally present some map markers.
This has been working nice, nonetheless I would like the map to disregard the secure areas of the machine in order that it stretches to the total width (behind any notches and backside tab bars).
The issue I am working into is that after I apply .ignoresSafeArea()
to the map, all of my map markers get off of, like so:
This solely occurs after I add .ignoresSafeArea()
.
I am undecided what I am doing improper.
Right here is my SwiftUI code that reveals the map:
import GoogleMaps
struct MapView: View {
@EnvironmentObject var apiManager: ApiManager
personal var mapOptions: GMSMapViewOptions {
var choices = GMSMapViewOptions()
return choices
}
var physique: some View {
ZStack {
GoogleMapView(choices: mapOptions)
.mapMarkers(MapHelper.convertToGoogleMarkers(markers: apiManager.markers))
.ignoresSafeArea()
}
.activity {
getMapMarkers()
}
.navigationBarHidden(true)
}
func getMapMarkers() {
Process {
await apiManager.fetchAllMapMarkers()
}
}
}
And right here is the UIViewRepresentable
for GoogleMapView
:
import SwiftUI
import GoogleMaps
/// A SwiftUI wrapper for GMSMapView that shows a map with non-obligatory markers and configurable map sort
struct GoogleMapView: UIViewRepresentable {
// Configuration properties - set at initialization
personal let choices: GMSMapViewOptions
/// Array of markers to show on the map
personal var markers: [GMSMarker]
/// Sort of map to show (regular, satellite tv for pc, hybrid, terrain)
personal let mapType: GMSMapViewType
// Runtime updatable properties
personal var digicam: GMSCameraPosition?
personal var backgroundColor: UIColor?
/// Shared delegate occasion to deal with map interactions throughout all cases
/// Utilizing static ensures callbacks work collectively when chaining modifiers
personal static let mapDelegate = GoogleMapViewDelegate()
init(choices: GMSMapViewOptions,
markers: [GMSMarker] = [],
mapType: GMSMapViewType = .regular) {
self.choices = choices
self.markers = markers
self.mapType = mapType
}
/// Creates the underlying UIKit map view
func makeUIView(context: Context) -> GMSMapView {
// Initialize map with present choices
let mapView = GMSMapView(choices: choices)
mapView.overrideUserInterfaceStyle = .unspecified
mapView.mapType = mapType
// Set shared delegate to deal with interactions
mapView.delegate = Self.mapDelegate
return mapView
}
/// Updates the map view when SwiftUI state modifications
func updateUIView(_ uiView: GMSMapView, context: Context) {
// Replace runtime properties if set
if let digicam = digicam {
uiView.digicam = digicam
}
if let backgroundColor = backgroundColor {
uiView.backgroundColor = backgroundColor
}
//clears all markers and polylines
uiView.clear()
// Refresh markers on the map
markers.forEach { marker in
marker.map = uiView
}
uiView.mapType = mapType // Replace map sort if modified
}
}
class GoogleMapViewDelegate: NSObject, GMSMapViewDelegate {
var tapHandler: ((CLLocationCoordinate2D) -> Void)?
var markerTapHandler: ((GMSMarker) -> Bool)?
/// Referred to as by GMSMapView when person faucets the map at a selected coordinate
/// - Parameters:
/// - mapView: The GMSMapView that detected the faucet
/// - coordinate: The geographic coordinate the place the faucet occurred
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
tapHandler?(coordinate) // Ahead faucet to handler if one is ready
}
/// Referred to as by GMSMapView when person faucets a marker on the map
/// - Parameters:
/// - mapView: The GMSMapView that detected the faucet
/// - marker: The GMSMarker that was tapped
/// - Returns: true if faucet was dealt with by the app, false to permit default marker habits
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
return markerTapHandler?(marker) ?? false // Ahead to handler or use default habits
}
}
// MARK: - viewModifiers and Markers
extension GoogleMapView {
/// Updates the digicam place of the map view throughout runtime
/// - Parameter place: New digicam place to use
/// - Returns: Up to date GoogleMapView occasion
func digicam(_ place: GMSCameraPosition?) -> GoogleMapView {
var view = self
if let place = place {
view.digicam = place
}
return view
}
/// Updates the background shade of the map view throughout runtime
/// - Parameter shade: New background shade to use
/// - Returns: Up to date GoogleMapView occasion
func backgroundColor(_ shade: UIColor) -> GoogleMapView {
var view = self
view.backgroundColor = shade
return view
}
/// Adjustments the map show sort
/// - Parameter sort: GMSMapViewType to make use of (.regular, .satellite tv for pc, and many others)
/// - Returns: New GoogleMapView occasion with up to date map sort
func mapType(_ sort: GMSMapViewType) -> GoogleMapView {
GoogleMapView(choices: choices, markers: markers, mapType: sort)
}
/// Provides markers to the map
/// - Parameter markers: Array of GMSMarker objects to show
/// - Returns: New GoogleMapView occasion with up to date markers
func mapMarkers(_ markers: [GMSMarker]) -> GoogleMapView {
var view = self
view.markers = markers
return view
}
}
// MARK: - View Callbacks
extension GoogleMapView {
/// Provides handler for map faucet occasions
/// - Parameter handler: Closure referred to as when map is tapped, offering faucet coordinates
/// - Returns: Similar GoogleMapView occasion with up to date faucet handler
func onMapTapped(_ handler: @escaping (CLLocationCoordinate2D) -> Void) -> GoogleMapView {
Self.mapDelegate.tapHandler = handler
return self
}
/// Provides handler for marker faucet occasions
/// - Parameter handler: Closure referred to as when marker is tapped
/// - Returns: Similar GoogleMapView occasion with up to date marker handler
/// Return true from handler to point faucet was dealt with
func onMarkerTapped(_ handler: @escaping (GMSMarker) -> Bool) -> GoogleMapView {
Self.mapDelegate.markerTapHandler = handler
return self
}
}
extension View {
/// Configures the view to disregard secure areas apart from the highest
/// Helpful for map views that ought to fill the display beneath standing bar
/// - Returns: Modified view that extends to display edges besides prime
func ignoresSafeAreaExceptTop() -> some View {
ignoresSafeArea(.container, edges: [.bottom, .horizontal])
}
}