[英]How to put Google Maps in TableView - Swift 3
我是Swift編程的新手。 我正在嘗試將Google Map放在UITableView
。 我可以這樣做嗎?
用戶界面應如下所示:
這是我使用Storyboard實現UITableView的代碼:
override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if section == 0 {
rowCount = 1
}
if section == 1 {
rowCount = arrayOfStatic.count
}
if section == 2 {
rowCount = arrayOfDynamic.count
}
return rowCount
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.section == 0 {
return 250
} else {
return 70
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
return cell
} else if indexPath.section == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "staticCellId", for: indexPath) as! StaticCell
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "placesCellId", for: indexPath) as! PlacesCell
let ip = indexPath
cell.imageFoursquarePlaces.image = arrayOfDynamic[indexPath.row].image
cell.labelPlacesName.text = arrayOfDynamic[ip.row].name
cell.labelPlacesCategory.text = arrayOfDynamic[ip.row].category
return cell
}
}
這里是我的實現代碼UITableViewCell
在首節UITableView
:
class GoogleMapsCell: UITableViewCell, GMSMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var googleMapsView: UIView!
@IBOutlet weak var buttonCurrentLoc: UIButton!
var googleMaps: GMSMapView!
var locationManager = CLLocationManager()
var camera = GMSCameraPosition()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.showCurrentLocationOnMap()
self.locationManager.stopUpdatingLocation()
}
func showCurrentLocationOnMap() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
let camera = GMSCameraPosition.camera(withLatitude: (self.locationManager.location?.coordinate.latitude)!, longitude: (self.locationManager.location?.coordinate.longitude)!, zoom: 15)
self.googleMaps = GMSMapView.map(withFrame: CGRect(x: 0,y: 0, width: self.googleMapsView.frame.size.width, height: self.googleMapsView.frame.height), camera: camera)
do {
if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
self.googleMaps.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
} else {
NSLog("Unable to find style.json")
}
} catch {
NSLog("The style definition could not be loaded: \(error)")
}
self.googleMaps.isMyLocationEnabled = true
self.googleMaps.accessibilityElementsHidden = false
self.addSubview(self.googleMaps)
self.googleMaps.camera = camera
self.addSubview(self.buttonCurrentLoc)
}
}
它給我結果:
我有一個searchBar
作為HeaderView
。 我試圖通過在TableView的第一個單元格內創建UIView
來放置Google Map View。 但是我無法顯示Google Map View,並且無法單擊該按鈕。 我怎樣才能做到這一點?
任何幫助,將不勝感激 :)
您忘記了在cellForRowAtIndexPath
中用GoogleMapsCell
的實例調用showCurrentLocationOnMap
方法。
let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell
//add this method call
cell.showCurrentLocationOnMap()
return cell
或者,您可以在GoogleMapsCell
覆蓋awakeFromNib
,並showCurrentLocationOnMap
一起調用showCurrentLocationOnMap
方法。
override func awakeFromNib() {
super.awakeFromNib()
self.showCurrentLocationOnMap()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.