iOS Development

ios – GMSMapView strikes digital camera to top-left as a substitute of heart

Spread the love


I need to use a Google map in a swiftUI mission and for that objective, I created a UIViewControllerRepresentable struct, the map is working nicely within the simulator, and CLLocationCoordinate2D is about to the middle of the map, however after I run it on an actual system the google map put the situation within the top-left nook as a substitute of the middle,
how can I put the situation precisely within the heart of the map display 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
    }
    
    personal func setStyleToMap(_ uiViewController: MapViewController) {
        do {
            if let styleURL = Bundle.foremost.url(forResource: "type", withExtension: "json") {
                uiViewController.map.mapStyle = attempt GMSMapStyle(contentsOfFileURL: styleURL)
            } else {
                //     DDLogWarn("Unable to search out type.json")
            }
        } catch {
            //DDLogWarn("A number of of the map kinds didn't 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 digital camera = GMSCameraPosition.digital camera(withTarget: coordinate, zoom: Float(selectedLocation.depth ?? 12))
        uiViewController.map.animate(to: /*GMSCameraUpdate.setCamera(*/ digital camera)
        
        
        uiViewController.map.clear()
        
        markers.forEach { marker in
            marker.map = uiViewController.map
        }
        
        if let selectedMarker = selectedMarker {
            let digital camera = GMSCameraPosition.digital camera(withTarget: selectedMarker.place, zoom: defaultZoomLevel)
            print("Animating to place (selectedMarker.place)")
            CATransaction.start()
            CATransaction.setValue(NSNumber(floatLiteral: 1), forKey: kCATransactionAnimationDuration)
            uiViewController.map.animate(with: GMSCameraUpdate.setCamera(digital camera))
            CATransaction.commit() }
        
    }
    
    func makeCoordinator() -> MapViewCoordinator {
        return MapViewCoordinator(self)
    }
    
    ultimate 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 place: GMSCameraPosition) {
            self.mapView.onAnimationEnded()
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *