简体   繁体   English

使用Google Map API获取当前位置

[英]Getting current location using Google map api

I'm trying to get current location of the user, however, the map gets stuck in UK. 我正在尝试获取用户的当前位置,但是地图卡在了英国。 I edited info.plist for the permission. 我编辑了info.plist以获得许可。 Here's my mapviewcontroller class and can anyone tell me what am I doing wrong? 这是我的mapviewcontroller类,谁能告诉我我在做什么错? Thanks 谢谢

import UIKit
import GoogleMaps

class MapViewController: UIViewController {

    var newMap: Bool!
    let locationManager =  CLLocationManager()
    var didFindMyLocation = false

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        self.locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest

    }


}

extension MapViewController: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didChangeAuthroizationStatus status: CLAuthorizationStatus) {
        if status == .AuthorizedAlways {

            locationManager.startUpdatingLocation()

        }
    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         let locValue: CLLocationCoordinate2D = manager.location!.coordinate
         let camera = GMSCameraPosition.cameraWithLatitude(locValue.latitude,
                                                      longitude: locValue.longitude, zoom: 6)
         let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)

         mapView.camera = GMSCameraPosition(target: locValue, zoom: 15, bearing: 0, viewingAngle: 0)

         mapView.myLocationEnabled = true
         mapView.settings.myLocationButton = true

         locationManager.stopUpdatingLocation()
    }

}

EDIT : I'm testing it on simulator and set the simulation location to cities other than London. 编辑:我正在模拟器上对其进行测试,并将模拟位置设置为伦敦以外的城市。

There's nothing wrong in my code. 我的代码没有错。 It is because I forgot to enable the Google Place API! 这是因为我忘记启用Google Place API!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM