【问题标题】:display a map if the table is empty如果表格为空,则显示地图
【发布时间】:2017-12-25 14:49:11
【问题描述】:

这是我尝试在 UITable 为空时显示地图的代码

var mapView:GMSMapView?

     func numberOfSections(in tableView: UITableView) -> Int {
        if orders.count > 0 {
            self.tableview.backgroundView = nil
            self.tableview.separatorStyle = .singleLine
            return 1
        }

        mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))


        //so the mapView is of width 200, height 200 and its center is same as center of the self.view
        mapView?.center = self.view.center
       self.tableview.backgroundView = mapView


        return 0
    }

但是项目崩溃了,原因是

libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 您能调试一下并告诉我哪条线路崩溃了吗?从 viewdidload 开始。
  • 并且还在控制台日志中显示完整的错误报告。
  • 我已经导入了 GoogleMaps 并在 AppDelegate 中使用了 Google Key 但我仍然有理由:'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'

标签: swift xcode google-maps uitableview swift4


【解决方案1】:

问题是无论之前的条件是什么,您都返回了0

func numberOfSections(in tableView: UITableView) -> Int {

    if orders.count > 0 {

        tableview.backgroundView = nil
        tableview.separatorStyle = .singleLine
        return 1

    } else {

        mapView = GMSMapView.map(withFrame: CGRect(x: 100, y: 100, width: 200, height: 200), camera: GMSCameraPosition.camera(withLatitude: 51.050657, longitude: 10.649514, zoom: 5.5))
        mapView?.center = view.center
        tableview.backgroundView = mapView
        return 0

    }

}

【讨论】:

  • 我已经导入了 GoogleMaps 并在 AppDelegate 中使用了 Google Key 但我仍然得到了reason: 'Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use'
【解决方案2】:

您似乎没有在 AppDelegate 中初始化 APIkey。只需按照此步骤即可。

在 AppDelegate 中导入此文件。

import GoogleMaps 
import GooglePlaces

然后在 didFinishLaunchingWithOptions 方法中添加 api 密钥。

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

您可以在注册 App 时从 Google Developers Console 获取 API Key。

欲了解更多信息,请查看此链接:- Developers Console

【讨论】:

  • 已经这样做了。我在其他 ViewController 中使用谷歌地图,它工作正常。但是在这个视图中,我得到了这个错误。
  • 我认为是因为 Google Maps 代码在 API 密钥初始化之前运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-08
  • 2014-09-06
  • 1970-01-01
  • 1970-01-01
  • 2013-05-27
  • 2017-09-28
  • 1970-01-01
相关资源
最近更新 更多