GMSMapView moves camera to top-left instead of center

48 Views Asked by At

I want to use a Google map in a swiftUI project and for that purpose, I created a UIViewControllerRepresentable struct, the map is working well in the simulator, and CLLocationCoordinate2D is set to the center of the map, but when I run it on a real device the google map put the location in the top-left corner instead of the center, how can I put the location exactly in the center of the map screen?




struct MapView: UIViewControllerRepresentable {
    
    @Binding var markers: [GMSMarker]
    @Binding var selectedMarker: GMSMarker?
    @Binding var selectedLocation: MapRegionAreaLocationModel
    var defaultZoomLevel: Float = 10
    let isUserInteractionEnabled: Bool
    
    var onAnimationEnded: () -> ()
    //var mapViewWillMove: (Bool) -> ()
    
    func makeUIViewController(context: Context) -> MapViewController {
        let uiViewController = MapViewController()
        uiViewController.map.delegate = context.coordinator
        setStyleToMap(uiViewController)
        return uiViewController
    }
    
    private func setStyleToMap(_ uiViewController: MapViewController) {
        do {
            if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
                uiViewController.map.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
            } else {
                //     DDLogWarn("Unable to find style.json")
            }
        } catch {
            //DDLogWarn("One or more of the map styles failed to load. \(error)")
        }
    }
    
    func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
        guard let newLatitude = selectedLocation.latitude, let newLongitude = selectedLocation.longitude else {
            return
        }
        let coordinate = CLLocationCoordinate2D(latitude: newLatitude, longitude: newLongitude)
        let camera = GMSCameraPosition.camera(withTarget: coordinate, zoom: Float(selectedLocation.depth ?? 12))
        uiViewController.map.animate(to: /*GMSCameraUpdate.setCamera(*/ camera)
        
        
        uiViewController.map.clear()
        
        markers.forEach { marker in
            marker.map = uiViewController.map
        }
        
        if let selectedMarker = selectedMarker {
            let camera = GMSCameraPosition.camera(withTarget: selectedMarker.position, zoom: defaultZoomLevel)
            print("Animating to position \(selectedMarker.position)")
            CATransaction.begin()
            CATransaction.setValue(NSNumber(floatLiteral: 1), forKey: kCATransactionAnimationDuration)
            uiViewController.map.animate(with: GMSCameraUpdate.setCamera(camera))
            CATransaction.commit() }
        
    }
    
    func makeCoordinator() -> MapViewCoordinator {
        return MapViewCoordinator(self)
    }
    
    final class MapViewCoordinator: NSObject, GMSMapViewDelegate {
        var mapView: MapView
        
        init(_ mapView: MapView) {
            self.mapView = mapView
        }
        
        func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
            
        }
        
        func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {
            self.mapView.onAnimationEnded()
        }
    }
}
0

There are 0 best solutions below