无法让我的位置显示在iOS 8的Google地图中

Isu*_*uru 5 location google-maps ios swift ios8

我正在尝试在iOS 8的Swift项目中使用Google Maps for iOS.我添加了Objective-C桥接标头,并按照其文档中的说明添加了Google Maps SDK .我尝试了这个例子,一切正常.

我需要显示我当前的位置.我在iOS 8模拟器中设置了自定义位置的坐标,并修改了 StackOverflow应答中显示的代码.下面是它的Swift版本.

import UIKit
import Foundation

class MapViewController: UIViewController {

    @IBOutlet var mapView: GMSMapView!

    var firstLocationUpdate: Bool?

    override func viewDidLoad() {
        super.viewDidLoad()

        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
    }

    override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafeMutablePointer<Void>) {
        firstLocationUpdate = true
        let location = change[NSKeyValueChangeNewKey] as CLLocation
        mapView.camera = GMSCameraPosition.cameraWithTarget(location.coordinate, zoom: 14)
    }
}
Run Code Online (Sandbox Code Playgroud)

我跑了但它并没有指向我指定的位置.当我println()离开该位置时println(mapView.myLocation),它返回nil.

我认为这是因为在iOS8中,我们明确要求用户允许我们获取他们的位置.看到这里.

我将NSLocationWhenInUseUsageDescription密钥添加到了info.plist中.我的问题是,CLLocationManager如果我使用Google Maps SDK,我该如何申请许可.我把下面的代码只是为了检查,但它没有提示权限对话框.

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
Run Code Online (Sandbox Code Playgroud)

有解决方法吗?或者我们是否要等到Google更新其SDK?

谢谢.

Pau*_*w11 7

您需要保留CLLocationManager,否则它将在有机会提供授权对话框之前释放.

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
}
Run Code Online (Sandbox Code Playgroud)