简体   繁体   English

无法在谷歌地图上获得当前位置(GPS)ios sdk

[英]Cant get current location (GPS) on google maps ios sdk

My goal is to move the camera a user's current location, but for somehow it keeps showing the general map, I have tried many things but seems like it wont move to the user's current location 我的目标是将相机移动到用户的当前位置,但是为了某种方式它不断显示一般地图,我尝试了很多东西,但似乎它不会移动到用户的当前位置

Screenshot 截图

在此输入图像描述

Current code 目前的代码

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

        }
    }
}

Did you add NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription (in your case it's this one) keys in your .plist file. 您是否在.plist文件中添加了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription (在您的情况下就是这一个)键。 Because if the alert view asking for authorization is not showing this might be the issue. 因为如果要求授权的警报视图未显示,则可能是问题所在。

Make sure you have imported and registered your GoogleMapsAPI Key properly: This is done in the AppDelegate 确保您已正确导入并注册了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
}

Afterwards, some where in a load fucntion within a ViewController file: 之后,在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)

Just a note: Google Maps API does NOT need a user autherization to use the map, however, CLLLocationManager DOES. 请注意:Google Maps API不需要用户自动化来使用地图,但CLLLocationManager也是如此。 On a load you should check to see if the permission is set or not, then go from there. 在加载时,您应该检查是否设置了权限,然后从那里开始。

First you need to setup these basic steps : 首先,您需要设置以下基本步骤:

Step 1: Import the this into your controller class 第1步:将其导入您的控制器类

import CoreLocation

Step 2: Add this delegate to your class file 第2步:将此委托添加到您的类文件中

class Your_controller: CLLocationManagerDelegate

Step 3: Declare this above for viewed load 第3步:声明以上内容以查看负载

var locationManager = CLLocationManager()

Step 4: Add this code to your viewdidload method 第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")
}

Step 5: Add this code below viewdidload method 第5步:在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)

} }

Step 6: add to permission your plist file 第6步:添加权限您的plist文件

Key - "Privacy - Location When In Use Usage Description" 密钥 - “隐私 - 使用时的位置使用说明”

Value - "This app needs access to your location" 价值 - “此应用需要访问您的位置”

Step 7: run your Application on Hardware Device 第7步:在硬件设备上运行您的应用程序

Step 8: Just call the this method when you want to show current location. 步骤8:当您想要显示当前位置时,只需调用此方法即可。

self.showCurrentLocation()

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

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