谷歌地图SDK,iOS,无法获取myLocation

谷歌地图SDK,iOS,无法获取myLocation,ios,swift,google-maps-sdk-ios,Ios,Swift,Google Maps Sdk Ios,我想使用谷歌地图SDK在iOS应用程序上显示我的位置。但是,它无法获取我的位置。我参考了以下文件 这是我的密码。它只显示了英国的地图 请帮我解决这个问题 import UIKit class SearchVC: UIViewController,CLLocationManagerDelegate{ ///Google Map @IBOutlet weak var mapView:GMSMapView! let locationManager = CLLocationManager() o

我想使用谷歌地图SDK在iOS应用程序上显示我的位置。但是,它无法获取我的位置。我参考了以下文件

这是我的密码。它只显示了英国的地图

请帮我解决这个问题

import UIKit

class SearchVC: UIViewController,CLLocationManagerDelegate{

///Google Map
@IBOutlet weak var mapView:GMSMapView!
let locationManager =  CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus){
    if status == .AuthorizedWhenInUse{

        locationManager.startUpdatingLocation()

        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
    }
}
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()
    }
}
}

这里有一些代码来解析你的位置。。。我认为您在提取地图视图要加载的位置信息时遇到了问题

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: { (placemarks, error) -> Void in
    if (error != nil) {
        println("Error:" + error.localizedDescription)
        return
    }

    if placemarks.count > 0 {
        let pm = placemarks[0] as CLPlacemark
        pm.location.coordinate;
        mapView.camera = GMSCameraPosition(target:pm.location.coordinate, zoom:15,bearing:0, viewingAngle:0)
        locationManager.stopUpdatingLocation()

    }else {
        println("Error with data")
    }
}
我还没有编译过这段代码,而且我也不是很精明,但希望这能有所帮助

要更改您的位置,请转到
编辑方案…

然后选择要模拟的任何位置
问题:

func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.startUpdatingLocation()

        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
    }
}
只有当授权状态更改时,才真正调用此函数。例如,当您第一次运行应用程序时,它会要求启用位置服务。一旦点击yes,此函数将运行。下次运行应用程序时,授权状态不会更改,因为它以前已启用

修复方法:

func locationManager(manager:CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)
override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.requestWhenInUseAuthorization()
    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.startUpdatingLocation()

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

现在,每次加载视图时,它都会开始更新位置,而不是在授权状态更改时。确保还将key:value对添加到info.plist(nslocationwhenUsageDescription:{authorizationOn message string})中

您是否修改了info.plist文件?是的。我将密钥添加为“NSLocationWhenUsageDescription”,值添加为“通过访问您的位置,此应用程序可以找到好餐馆”。didUpdateLocations是否返回位置?还是只是默默地失败?你有模拟的位置吗?didUpdateLocations返回一个位置,但它的位置是UK,可能纬度=0.0,经度=0.0。在
GMSCameraPosition()
中键入。确保在
pm.location.coordinate
之前包含
target:
,我已按照您所说的修改了代码,结果已更改。然而,当我在iOS模拟器上按下我的位置按钮时,它显示了苹果地址作为我的位置。你能告诉我如何解决这个问题吗?只有少数几个地方你可以通过你的iOS模拟器来模拟。。。所以,你能真正获得准确位置的唯一方法就是在你的手机上运行应用程序,并通过这种方式获得位置。。。