当前位置: 首页 > 面试题库 >

无法获取我的位置以显示在iOS 8的Google地图中

甘兴学
2023-03-14
问题内容

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

我需要显示我的当前位置。我在iOS8模拟器中为自定义位置设置了坐标,并修改了此答案中所示的代码。下面是它的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)
    }
}

我运行了它,但是它没有指向我分配的位置。当我println()离开该位置时println(mapView.myLocation),它会返回nil

我认为这是因为在iOS8中,我们必须明确要求用户允许我们获取其位置。

我将NSLocationWhenInUseUsageDescription密钥添加到了info.plist中。我的问题是CLLocationManager由于我使用的是Google
Maps SDK,因此我该如何请求获得许可。我将以下代码用于检查,但未提示权限对话框。

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()

有解决方法吗?还是我们必须等到Google更新其SDK?

谢谢。


问题答案:

您需要保留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
}


 类似资料:
  • 问题内容: 最后,我成功显示了地图,现在,我想显示我的当前位置,我尝试使用这些代码,但是当我单击右上角的我的位置按钮时,它不起作用。 AndroidManifest.xml MapsActivity.java 问题答案: 将此代码用于当前位置:

  • 我到处寻找这个问题的答案,在任何论坛问题上都没有人能提供帮助。我已经搜索了教程。API指南指出: 仅当启用我的位置层时,我的位置按钮才会出现在屏幕的右上角。 所以我一直在寻找这个我的位置层,但一直找不到任何东西。如何在Google地图上显示我的位置?

  • 本文向大家介绍js+html5获取用户地理位置信息并在Google地图上显示的方法,包括了js+html5获取用户地理位置信息并在Google地图上显示的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了js+html5获取用户地理位置信息并在Google地图上显示的方法。分享给大家供大家参考。具体实现方法如下: 希望本文所述对大家的web程序设计有所帮助。

  • 问题内容: 我正在开发一个包含3个活动的android应用程序,其中一个是map。我正在使用谷歌地图 我想要的是: 当用户打开活动(地图)时,它将向用户显示她的位置而不是坐标。 用户还应该能够通过使用大头针定位(固定)任何地方,而我的应用程序必须将此大头针的坐标存储在变量中,以便以后使用。 这是CustomPinpoint类 问题答案: 我已经实现了您想要的相同的东西。我为您提供示例代码。在这种情

  • 我试图使用Java客户机将数据从S3传输到GCS,但我遇到了这个错误。 由于权限不足,无法获取Google云存储(GCS)存储桶的位置。请验证是否已授予必要的权限。 我正在使用具有项目所有者角色的服务号,它应该授予对所有项目资源的无限制访问权限。

  • 本文向大家介绍如何最好地在Google地图上显示HTML5地理位置的准确性?,包括了如何最好地在Google地图上显示HTML5地理位置的准确性?的使用技巧和注意事项,需要的朋友参考一下 要在Google Map上显示HTML5地理位置的准确性,请使用以下代码-