2016-06-07 10 views
0

In einem benutzerdefinierten tableViewCell, schließe ich eine Ansicht, die eine Google-Karte Klasse auf die benutzerdefinierten Klasse istWie aktiviere ich eine Google Maps-Schaltfläche in einer Zelle?

aber es scheint, dass diese Codezeile nicht in der benutzerdefinierten Zelle, die ich

Diese Zelle erstellt läuft Teil eines Tableview ist, habe ich versucht, diese Zelle zum Beispiel instanziiert var gmapsCell = GMapTableViewCell im viewDidLoad des Tableview aber es scheint, wird es immer nil

Also ich unten Codes meinen Ansatz ändern

import UIKit 
import GoogleMaps 

class GMapTableViewCell: UITableViewCell, GMSMapViewDelegate { 

    let locationManager = CLLocationManager() 

    @IBOutlet var mapView: GMSMapView! 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     locationManager.requestAlwaysAuthorization() 
     // For use in foreground 
     locationManager.requestWhenInUseAuthorization() 
     mapView.delegate = self 
     mapView.settings.myLocationButton = true 

    } 


    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() 

     } 
    } 
} 

Das Problem ist im Moment, dass

override func awakeFromNib() { 
     super.awakeFromNib() 
     locationManager.requestAlwaysAuthorization() 
     // For use in foreground 
     locationManager.requestWhenInUseAuthorization() 
     mapView.delegate = self 
     mapView.settings.myLocationButton = true 

    } 

Das wird nie laufen. Mein Ziel ist einfach, zum aktuellen Standort des Geräts zu gelangen. Gibt es eine Entsprechung von viewDidLoad für benutzerdefinierte Zellen?

Antwort

0

Sie müssen "NSLocationWhenInUseUsageDescription" -Schlüssel als Zeichenfolge in info.plist hinzufügen, damit der Standortmanager die Aktualisierung der Standorte starten kann. enter image description here

+0

habe ich schon vor langer Zeit hinzugefügt. – airsoftFreak

0

Der gesamte Code innerhalb von awakeNib() sollte in tableView: cellForRowAtIndexPath geschrieben werden. Stellen Sie alle Delegierten dort ein. Es wird dann gut funktionieren.

Verwandte Themen