【问题标题】:Google Map SDK, iOS , cannot get myLocationGoogle Map SDK,iOS,无法获取 myLocation
【发布时间】:2014-12-20 09:13:07
【问题描述】:

我想使用 Google Maps SDK 在 iOS 应用上显示我的位置。但是,它无法获取我的位置。我参考了以下文件,document1document2

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

请帮我解决问题。

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

【问题讨论】:

  • 您是否修改了 info.plist 文件?
  • 是的。我将键添加为“NSLocationWhenInUseUsageDescription”,将值添加为“通过访问您的位置,此应用可以找到好餐馆”。
  • didUpdateLocations 是否返回位置?还是它只是默默地失败?你有模拟位置吗?
  • didUpdateLocations 返回一个位置,但它的位置是英国,可能纬度=0.0,经度=0.0。

标签: ios swift google-maps-sdk-ios


【解决方案1】:

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

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

我还没有编译这段代码,我也不是很熟练,但希望这会有所帮助

要更改您的位置,请转到Edit Scheme...

然后选择您要模拟的任何位置

【讨论】:

  • GMSCameraPosition() 中打错了字。确保在pm.location.coordinate之前包含target:
  • 我按照你说的修改了代码,结果变了。但是,当我在 iOS 模拟器上按下我的位置按钮时,它会将 Apple 地址显示为我的位置。您能告诉我如何解决这种情况吗?
  • 只有少数几个位置可以通过您的 iOS 模拟器模拟...因此,您真正获得确切位置的唯一方法是在手机上运行应用程序并以这种方式获得位置。 ..
【解决方案2】:

问题:

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 (NSLocationWhenInUseUsageDescription: {authorizatinon message string})。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多