ios - Google Maps iOS SDK 不会在 tableViewCell 中更新

标签 ios swift google-maps uitableview google-maps-sdk-ios

我在普通 View Controller 上运行 google maps ios SDK,它运行完美,它甚至显示了当前位置,但我决定放入一个 tableViewCell,下面的代码将显示如何

import UIKit
import GoogleMaps

class GMapTableViewCell: UITableViewCell, GMSMapViewDelegate  {

    let locationManager = CLLocationManager()

    @IBOutlet var mapView: GMSMapView!
    override func awakeFromNib() {
        super.awakeFromNib()
    }


    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }



}


extension GMapTableViewCell: CLLocationManagerDelegate {

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        if status == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation()
            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.first {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationManager.stopUpdatingLocation()

        }
    }
}

这段代码所做的只是添加一个 google maps delegate 并扩展一个 CLLocationManagerDelegate。在扩展中,这是它应该更新设备当前位置的地方,它再次在正常的 UIView 中完美运行,但不是在这里。

tableView中的代码

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


            let reuseIdentifier = "maps"
            let cell = tableView.dequeueReusableCellWithIdentifier(reuseIdentifier, forIndexPath: indexPath) as! GMapTableViewCell
            // I inherit both GMSMapViewDelegate, CLLocationManagerDelegate, for tableView 
            cell.locationManager.delegate = self
            cell.locationManager.requestAlwaysAuthorization()
            cell.locationManager.requestWhenInUseAuthorization()
            cell.mapView.delegate = self
            cell.mapView.myLocationEnabled = true
            cell.mapView.settings.myLocationButton = true

            return cell
    }

我不知道我做错了什么

最佳答案

我现在遇到了同样的问题,无法在 UITableViewCell 中放置任何 map 标记。

我发现,只有在单元格的 awakeFromNib 方法中调用 Google map 的方法时,Google map 才能在 UITableViewCell 中工作。

一直没搞清楚为什么,现在才发现。

关于ios - Google Maps iOS SDK 不会在 tableViewCell 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37697355/

相关文章:

swift - 使用递归枚举评估 (5 + 4) * 2 的更简单方法

Android - 谷歌地图 +-180 经度和 90 纬度

android - 图片解码失败。提供的图像必须是位图

google-maps - Google Places API 再次按城市搜索

ios - 将离线更改与核心数据中的原始数据分开保存

ios - 如何在不使用任何形式的查找表的情况下在 Swift for iOS 8 中列出(几乎)所有表情符号?

ios - 下载PDF文件并保存在文档目录中

ios - 如何在发布前查看iOS App Store URL?

iphone - 在 facebook ios6 中未进入 if(granted ) 循环 "Sign in with Facebook"

ios - Deeplink 仅打开应用程序但不打印快速操作所需的组件