当前位置无法在Google地图中使用

Dha*_*raj 2 google-maps cllocationmanager ios swift swift3

我已经在swift 3中整合了谷歌地图,当地图画面出现时没有显示当前位置,我在.plist文件中添加了两个键,还设置了CLLocationManager委托和requestAlwaysAuthorization

class MapViewController: UIViewController, CLLocationManagerDelegate, UITextFieldDelegate {

    @IBOutlet var mapView: GMSMapView!
    var marker: GMSMarker?


    override func viewDidLoad() {
         super.viewDidLoad()
         self.title = "MapVC"
         self.doSetupUI()
         self.searchLocation()
   }

   override func viewWillAppear(_ animated: Bool) {
         let locationManager : CLLocationManager = CLLocationManager()
         locationManager.delegate = self
         locationManager.requestAlwaysAuthorization()
   }



func doGoogleMapSetup(lat : Double , lng : Double) {
    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude:lng, zoom:16)
    let mapView = GMSMapView.map(withFrame: .zero, camera:camera)
    mapView.isMyLocationEnabled = true

    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: lat, longitude: lng)
    marker.snippet = ""
    marker.appearAnimation = kGMSMarkerAnimationPop
    marker.map = mapView

    let arrPoints : NSMutableArray = NSMutableArray()
    arrPoints.add(UserDefaults.standard.object(forKey: "addressPoints"))

    for i in 0..<arrPoints.count {
        let path : String = (arrPoints.object(at: i)as! NSMutableArray).object(at: 0) as! String
        let route : GMSPath = GMSPath.init(fromEncodedPath: path)!
        let polyLine : GMSPolyline = GMSPolyline.init(path: route)
        polyLine.strokeWidth = 2.0
        polyLine.strokeColor = UIColor.red
        polyLine.map = mapView
    }
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*ari 22

为了显示当前位置,我们不需要任何位置管理器GoogleMaps.我们所需要的只是在.plist中添加其中一个键或两者.所以确保钥匙在那里.我用过NSLocationWhenInUseUsageDescription钥匙.

<key>NSLocationWhenInUseUsageDescription</key>
    <string>Allow location</string>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

还要确保您已调用GMSServices provideAPIKey方法并替换为您在google开发人员控制台中生成的API_KEY.此外,还应启用根据要求的所有相关Google API.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       // Override point for customization after application launch.
       GMSServices.provideAPIKey("YOUR_API_KEY")
       return true
    }
Run Code Online (Sandbox Code Playgroud)

所以,我假设您已经在谷歌开发者控制台中完成了所有设置和事情.

只需在控制器中写下以下行,GoogleMap就可以显示位置允许/禁止提示并获取用户的权限.

mapView.isMyLocationEnabled = true
Run Code Online (Sandbox Code Playgroud)

但是,这不会将地图设置为当前位置的动画.但您可以手动拖动地图以检查当前位置,您将在当前位置看到一个蓝点.

但是现在我们还想在加载时动画到当前位置ViewController.现在需要CLLocationManager到达.因此,在其didUpdateLocation委托中,我们可以获取当前位置,并且可以将图形设置为当前位置的动画.

所以这是我完整的控制器.

import UIKit
import GoogleMaps

class ViewController: UIViewController,GMSMapViewDelegate,CLLocationManagerDelegate {
    @IBOutlet weak var mapView: GMSMapView!

    var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView.isMyLocationEnabled = true
        mapView.delegate = self

        //Location Manager code to fetch current location
        self.locationManager.delegate = self
        self.locationManager.startUpdatingLocation()
    }

    //Location Manager delegates
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        let location = locations.last

        let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
        mapView.animate(to: camera)

        //Finally stop updating location otherwise it will come again and again in this delegate
        self.locationManager.stopUpdatingLocation()

    }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是不使用didUpdateLocation和不使用位置管理器只是使用GMSMapViewDelegate委托方法mapViewDidFinishTileRendering

func mapViewDidFinishTileRendering(_ mapView: GMSMapView) {
    let location = mapView.myLocation
    let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude:(location?.coordinate.longitude)!, zoom:14)
    mapView.animate(to: camera)
}
Run Code Online (Sandbox Code Playgroud)

每次完成地图渲染时都会调用它.
但是这有一个限制,每当您使用地图播放时,每当您拖动/捏合/缩放地图时,它总会将您带到当前位置.所以,你可以在bool这里实现某种变量逻辑.

您可以使用以获取您的位置

let yourCurrentLocation = mapView.myLocation
Run Code Online (Sandbox Code Playgroud)

确保在设备而不是模拟器上执行此操作.如果您使用的是模拟器,则必须选择一些自定义位置,然后才能看到蓝点.

在此输入图像描述

我已经给出了这种答案.检查此链接.但那是在Swift 2.x中.我在这个答案中发布的那个是在Swift 3.x中