【问题标题】:myLocationButton doesn't show on Google MapsmyLocationButton 未在 Google 地图上显示
【发布时间】:2015-04-21 07:40:04
【问题描述】:

我已按照如何通过 Google Maps SDK 获取用户位置的教程中的步骤进行操作。我也使用了Ray Wenderlich 提供的谷歌教程,但没有用。 位置按钮甚至不显示。

任何帮助都会很棒,谢谢。 这是我的代码:

    class NearestDealerViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mainMenuLabel: UILabel!
    @IBOutlet weak var nearestDealer: UILabel!
    @IBOutlet weak var mapView: GMSMapView!
    let locationManager = CLLocationManager()

    let transitionDelegate = TransitionDelegate()
    var delegate: NearestDealerViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        println(mapView.myLocationEnabled)
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedWhenInUse {
            locationManager.startUpdatingLocation()
            mapView.myLocationEnabled = true
            mapView.settings.myLocationButton = true
        }
        println(mapView.myLocationEnabled)

    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        if let location = locations.first as? CLLocation {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
            locationManager.stopUpdatingLocation()
        }
    }

    @IBAction func backTapped(sender: AnyObject) {
        delegate?.dismissNearestDealerViewController()
    }
}

【问题讨论】:

  • 我认为您的地图在位置更新时未分配,因此在调用 didChangeAuthorizationStatus 时 nslog 地图视图
  • @chiragshah 我的 mapView 已连接到我的故事板。而且我还听取了您的建议,对我的 mapView 进行 nslog,它不是零。
  • 你是在模拟器中运行这个项目然后你尝试改变位置。
  • 在模拟器上你必须通过调试来改变你的位置 -> 位置它启用你的位置按钮
  • 应该没问题,反正苹果模拟器在我的位置和它的按钮上存在已知问题。仍有疑问时,请进行测试。只需将其侧面加载到测试设备上并检查即可。

标签: ios google-maps swift cllocation


【解决方案1】:

你应该在 viewDidLoad() 下一行代码:

mapView.isMyLocationEnabled = true

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 2019-08-08
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    相关资源
    最近更新 更多