2017-01-24 24 views
0

Ich bin neu in der Swift-Programmierung. Ich versuche, Google Map in UITableView zu setzen. Kann ich das machen?Wie Sie Google Maps in TableView einfügen - Swift 3

Die Benutzeroberfläche sollte wie folgt sein:

enter image description here

Hier ist mein Code UITableView für die Umsetzung der Storyboard mit:

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 
    } 
} 

Hier ist mein Code für die Implementierung UITableViewCell im ersten Abschnitt 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) 
    } 

} 

Es gibt mir Ergebnis:

enter image description here

Ich habe einen searchBar als HeaderView. Ich habe versucht, Google Map View durch Erstellen einer UIView innerhalb der ersten Zelle von TableView zu erstellen. Aber ich kann die Google Map-Ansicht nicht anzeigen und die Schaltfläche kann nicht angeklickt werden. Wie kann ich das machen?

Jede Hilfe würde geschätzt :)

+0

Konfigurieren Sie die gewünschten Delegaten in der Zelle? –

+0

@SyedAliSalman, ich habe 'GMSMapViewDelegate' und' CLLocationManagerDelegate' für die 'GoogleMapsCell' Klasse implementiert. – user1994

Antwort

0

Sie haben vergessen showCurrentLocationOnMap Methode aufzurufen mit GoogleMapsCell ‚s Instanz in cellForRowAtIndexPath.

let cell = tableView.dequeueReusableCell(withIdentifier: "mapsCellId", for: indexPath) as! GoogleMapsCell 

//add this method call 
cell.showCurrentLocationOnMap() 

return cell 

Oder Sie können mit ihm awakeFromNib in Ihrem GoogleMapsCell und rufen Sie die showCurrentLocationOnMap Methode überschreiben.

override func awakeFromNib() { 
    super.awakeFromNib() 
    self.showCurrentLocationOnMap() 
} 
+0

oh, ja! Ich habe vergessen, diesen Teil zu schreiben. Vielen Dank! – user1994

+0

@ user1994 Willkommen Kumpel :) –

Verwandte Themen