无法让我的位置在iOS 8的Google地图中显示 [英] Cannot get my location to show up in Google Maps in iOS 8

查看:137
本文介绍了无法让我的位置在iOS 8的Google地图中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在iOS 8的Swift项目中使用Google Maps for iOS。我添加了Objective-C桥接标头并添加了Google Maps SDK,如文档。我尝试了这个示例,并且它工作正常。

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 documentation. And I tried this example and it works all fine.

我需要显示我的当前位置。我在我的iOS 8模拟器中设置自定义位置的坐标,并修改了代码,如 StackOverflow答案中所示。

I needed to show my current location. I set coordinates for a Custom Location in my iOS 8 simulator and modified the code as shown in this StackOverflow answer. Below is the Swift version of it.

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



<我跑了它,但它并没有指向我分配的位置。当我 println()出位置 println(mapView.myLocation)时,它返回 nil

我假设这是因为在iOS8中,我们明确地要求用户允许我们获取他们的位置。请参阅此处

I assume this is because in iOS8, we explicitly have to ask the user to allow us to get their location. See here.

我添加了 NSLocationWhenInUseUsageDescription 键到我的info.plist。我的问题是,我使用Google Maps SDK后,如何要求 CLLocationManager 的权限。

I added the NSLocationWhenInUseUsageDescription key to my info.plist. My problem is how can I ask for the permission on CLLocationManager since I'm using Google Maps SDK. I put the below code just to check but it didn't prompt the permission dialog.

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

有没有解决方法呢?或者我们必须等到谷歌更新他们的SDK?

Is there a workaround this? Or do we have to wait until Google update their SDK?

谢谢。

推荐答案

您需要保留您的CLLocationManager,否则会在获得授权对话框之前发布。

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
}

这篇关于无法让我的位置在iOS 8的Google地图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆