无法在Google Maps iOS SDK上获取当前位置(GPS)

3

我的目标是将相机移动到用户当前位置,但不知何故它一直显示一般的地图,我尝试了很多方法,但似乎它不会移动到用户的当前位置。

截图

enter image description here

当前代码

import UIKit
import GoogleMaps


class ParcelViewController: UIViewController, GMSMapViewDelegate {


    @IBOutlet var mapView: GMSMapView!

    let locationManager = CLLocationManager()

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

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

}


// Mark: -CLLocationManagerDelegate
extension ParcelViewController: CLLocationManagerDelegate {

    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: [CLLocation]) {
        if let location = locations.first {
            mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)

            locationManager.stopUpdatingLocation()

        }
    }
}

您最初会收到权限提示。 - Anbu.Karthik
我该怎么做? - airsoftFreak
尝试在设备上执行一次。 - Anbu.Karthik
我不明白我应该做什么。 - airsoftFreak
请检查此方法 didChangeAuthorizationStatus 的状态。 - Anbu.Karthik
状态为CLAuthorizationStatus。 - airsoftFreak
3个回答

3

您是否在您的.plist文件中添加了NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription键。如果授权提示框没有显示,这可能是问题所在。


这是完美的答案! - airsoftFreak

0
确保您已正确导入和注册您的GoogleMapsAPI密钥:这是在AppDelegate中完成的。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    GMSServices.provideAPIKey("[your key from Google API]")

    return true
}

之后,在ViewController文件中的某个加载函数中:

let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)      
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1))
self.map.setRegion(region, animated: true)

注意:Google Maps API不需要用户授权来使用地图,但CLLLocationManager需要。在加载时,您应该检查权限是否设置,然后再继续。


问题在于获取设备当前位置时遇到了困难。 - airsoftFreak
尝试使用LocationManager的三行代码。如果没有任何反应,那么您需要按照Chajmz的建议进行操作。在获得授权之后,上述代码将会生效。 - dovedevic

0

首先,您需要设置这些基本步骤:

步骤1:将此导入到您的控制器类中

import CoreLocation

步骤2:将此委托添加到您的类文件中

class Your_controller: CLLocationManagerDelegate

步骤3:在已加载的视图上方声明此内容

var locationManager = CLLocationManager()

步骤4:将此代码添加到您的viewdidload方法中

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


if CLLocationManager.locationServicesEnabled() {
  switch (CLLocationManager.authorizationStatus()) {
    case .notDetermined, .restricted, .denied:
      print("No access")
    case .authorizedAlways, .authorizedWhenInUse:
      print("Access")
  }
} else {
  print("Location services are not enabled")
}

第五步:在viewdidload方法下方添加以下代码。
func showCurrentLocation() {
    mapView.settings.myLocationButton = true
    let locationObj = locationManager.location as! CLLocation
    let coord = locationObj.coordinate
    let lattitude = coord.latitude
    let longitude = coord.longitude
    print(" lat in  updating \(lattitude) ")
    print(" long in  updating \(longitude)")

    let center = CLLocationCoordinate2D(latitude: locationObj.coordinate.latitude, longitude: locationObj.coordinate.longitude)
    let marker = GMSMarker()
    marker.position = center
    marker.title = "current location"
    marker.map = mapView
    let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: lattitude, longitude: longitude, zoom: Float(zoomLevel))
    self.mapView.animate(to: camera)

}

第6步:将您的plist文件添加到权限中
键 - “隐私 - 使用时定位使用说明”
值 - “此应用程序需要访问您的位置”
第7步:在硬件设备上运行您的应用程序
第8步:只需在您想要显示当前位置时调用此方法。
self.showCurrentLocation()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接