Skip to content

Error while initializing map on IOS device #53

@yiannis-spyridakis

Description

@yiannis-spyridakis

Bug Report

I am getting a crash while running ann app that uses @capacitor/google-maps on a test device (iPhone 8). The app crashes in Map.enableCurrentLocation and the error is:

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Which is caused by self.mapViewController.GMapView not being initialized.

Plugin(s)

@capacitor/google-maps

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 6.1.2
  @capacitor/core: 6.1.2
  @capacitor/android: 6.1.2
  @capacitor/ios: 6.1.2

Installed Dependencies:

  @capacitor/cli: 6.1.2
  @capacitor/core: 6.1.2
  @capacitor/android: 6.1.2
  @capacitor/ios: 6.1.2

[success] iOS looking great! 👌
[success] Android looking great! 👌

Platform(s)

IOS

Current Behavior

Crash on (I am guessing) certain device/ app setup combinations when loading map

Expected Behavior

Fix

Code Reproduction

I have debugged the plugin and the issue originates in Map.getTargetContainer

    private func getTargetContainer(refWidth: Double, refHeight: Double) -> UIView? {
        if let bridge = self.delegate.bridge {
            for item in bridge.webView!.getAllSubViews() {
                let isScrollView = item.isKind(of: NSClassFromString("WKChildScrollView")!) || item.isKind(of: NSClassFromString("WKScrollView")!)
                let isBridgeScrollView = item.isEqual(bridge.webView?.scrollView)

                if isScrollView && !isBridgeScrollView {
                    (item as? UIScrollView)?.isScrollEnabled = true

                    let height = Double((item as? UIScrollView)?.contentSize.height ?? 0)
                    let width = Double((item as? UIScrollView)?.contentSize.width ?? 0)
                    let actualHeight = round(height / 2)

                    let isWidthEqual = width == self.config.width
                    let isHeightEqual = actualHeight == self.config.height

                    if isWidthEqual && isHeightEqual && item.tag < self.targetViewController?.tag ?? Map.MAP_TAG {
                        return item
                    }
                }
            }
        }

        return nil
    }

In the function above isHeightEqual is always false. In the case of the 'correct' view it is off by one (367.0 <> 366.0) which, I am guessing has to do with the specific dimensions of my device and some rounding error.

This works for me:

                    let tolerance = 1.0
                    let isWidthEqual = abs(width - self.config.width) <= tolerance
                    let isHeightEqual = abs(actualHeight - self.config.height) <= tolerance

                    if isWidthEqual && isHeightEqual && item.tag < self.targetViewController?.tag ?? Map.MAP_TAG {
                        return item
                    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions