skip to Main Content

I’m getting an EXC_BAD_ACCESS (code=1, address=0x8) error when trying to add a polyline to my Google Map.

If I comment out the line polyline.map = mapView, everything works just fine.

Given the vagueness of the error, I’m unsure as to the root cause, or whether it’s just because the Simulator I’m running the app on is so slow. I’ve not been able to test it on a real device yet.

let path = GMSMutablePath()
for coordinate in route.polyline.geoJsonLinestring.coordinates {
    path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
    print(coordinate)
}

let bounds = GMSCoordinateBounds(path: path)
let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)

DispatchQueue.main.async { [self] in
    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor(named: "AccentColor")!
    polyline.strokeWidth = 5
    polyline.map = mapView
    
    mapView?.moveCamera(cameraUpdate)
}

The crash doesn’t necessarily happen straight away – when I zoom the map out after adding the polyline, that’s when it seems to crash most often.

Update:

I tried changing the GPU Frame Capture setting in Edit Scheme but it had no impact.

I also created a simple view with a map and a hardcoded 2 coordinate polyline, and it still crashes. This is literally it:

override func viewDidLoad() {
    super.viewDidLoad()
    
    var coordinates = [
        [-2.9287110999999997, 54.8929171],
        [-2.8932368, 54.8968314],
    ]
    let path = GMSMutablePath()
    for coordinate in coordinates {
        path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
    }
    
    let bounds = GMSCoordinateBounds(path: path)
    let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)
    let polyline = GMSPolyline(path: path)
    polyline.strokeColor = UIColor(named: "AccentColor")!
    polyline.strokeWidth = 10
    polyline.map = mapView
    
    mapView?.moveCamera(cameraUpdate)
}

Crashes after the map fully loads, which takes about a minute.

The performance of Google Maps in the simulator in general is terrible. The map view always seems to take >1 minute to fully load (grey view with Google logo then the tiles eventually appear).

2

Answers


  1. Chosen as BEST ANSWER

    This appears to be a bug, as I can reproduce it with the maps-sdk-for-ios-samples demo app. I've filed a bug report here: https://issuetracker.google.com/issues/323404687


  2. You are simply setting your polyline at a wrong place. If you set a line at viewDidLoad, your map won’t be there, and the app will crash. You should set it at viewDidAppear.

    import UIKit
    import GoogleMaps
    import MapKit
    
    class HomeViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
        // MARK: - Variables
        private let locationManager = CLLocationManager()
        
        // MARK: - IBOutlets
        @IBOutlet weak var mapView: GMSMapView!
        
        // MARK: - Life cycle
        override func viewDidLoad() {
            super.viewDidLoad()
            
            MakeButtonStyle.defaultButtonStyle(button: createButton, title: "Create")
            MakeButtonStyle.defaultButtonStyle(button: editButton, title: "Edit")
            MakeButtonStyle.defaultButtonStyle(button: deleteButton, title: "Delete")
            
            /* mapView and locationManager */       
            mapView.delegate = self
            mapView.isMyLocationEnabled = true
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            locationManager.startUpdatingLocation()
        }
        
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            
            let coordinates = [
                [-2.9287110999999997, 54.8929171],
                [-2.8932368, 54.8968314],
            ]
            let path = GMSMutablePath()
            for coordinate in coordinates {
                path.add(CLLocationCoordinate2D(latitude: coordinate[1], longitude: coordinate[0]))
            }
            let bounds = GMSCoordinateBounds(path: path)
            let cameraUpdate = GMSCameraUpdate.fit(bounds, withPadding: 16)
            let polyline = GMSPolyline(path: path)
            polyline.strokeColor = UIColor.red
            polyline.strokeWidth = 10
            polyline.map = mapView
            mapView?.moveCamera(cameraUpdate)
        }
        
        // MARK: - locationManager delegate
        func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
            guard status == .authorizedWhenInUse else {
                return
            }
            
            locationManager.startUpdatingLocation()
            mapView.isMyLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
        
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
            guard let location = locations.first else {
                return
            }
            
            let cameraPosition = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            mapView.camera = cameraPosition
            locationManager.stopUpdatingLocation()
            reverseGeocodeCoordinate(cameraPosition.target)
        }
    }
    

    The following is a screenshot from my iPhone sim.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search