在设备上运行时,GMSPlace Picker中的应用程序崩溃。(iOS 8.3)

时间:2015-12-29 11:12:43

标签: ios swift google-maps gmsplace

我使用以下代码来按照Google的建议选择位置

https://developers.google.com/places/ios-api/placepicker

    var placePicker: GMSPlacePicker?

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.pickPlace()
    }

  func pickPlace()
    {
        let center = CLLocationCoordinate2DMake(51.5108396, -0.0922251)
        let northEast = CLLocationCoordinate2DMake(center.latitude + 0.001, center.longitude + 0.001)
        let southWest = CLLocationCoordinate2DMake(center.latitude - 0.001, center.longitude - 0.001)
        let viewport = GMSCoordinateBounds(coordinate: northEast, coordinate: southWest)
        let config = GMSPlacePickerConfig(viewport: viewport)
        placePicker = GMSPlacePicker(config: config)

        placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: NSError?) -> Void in
            if let error = error
            {
                print("Pick Place error: \(error.localizedDescription)")
                return
            }

            if let place = place
            {
                print("Place name \(place.name)")
                print("Place address \(place.formattedAddress)")
                print("Place attributions \(place.attributions)")
                self.navigationController?.popToRootViewControllerAnimated(true)


            } else
            {
                print("No place selected")
            }
        })
    }

enter image description here 这段代码的问题是它在模拟器上完美运行,但在设备iOS 8.3上它崩溃而没有给出任何消息

1 个答案:

答案 0 :(得分:0)

将NSError更改为错误

placePicker?.pickPlaceWithCallback({ (place: GMSPlace?, error: Error?)

在AppDelegate类

中导入这两者
import GoogleMaps
import GooglePlaces

并且,您需要在didFinishLaunchingWithOptions

中设置它们
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        GMSServices.provideAPIKey("Your key")
        GMSPlacesClient.provideAPIKey("Your key")

        return true
    }

我在模拟器中测试后才能看到使用设备的错误消息