Cannot get my location to show up in Google Maps in iOS 8

后端 未结 1 884
再見小時候
再見小時候 2020-12-10 18:31

I\'m trying to use the Google Maps for iOS in a Swift project for iOS 8. I added the Objective-C bridging header and added the Google Maps SDK as instructed in their documen

相关标签:
1条回答
  • 2020-12-10 19:05

    You need your CLLocationManager to be retained, otherwise it is released before it gets a chance to present the authorisation dialog.

    class MapViewController: UIViewController {
    
    @IBOutlet var mapView: GMSMapView!
    
    var firstLocationUpdate: Bool?
    let locationManager=CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        self.locationManager.requestWhenInUseAuthorization()
    
        let camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 12)
        mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.settings.compassButton = true
        mapView.settings.myLocationButton = true
    
        mapView.addObserver(self, forKeyPath: "myLocation", options: .New, context: nil)
    
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            self.mapView.myLocationEnabled = true
        })
        println(mapView.myLocation)
        view = mapView
    
        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
        marker.title = "Sydney"
        marker.snippet = "Australia"
        marker.map = mapView
    }
    
    0 讨论(0)
提交回复
热议问题