gpt4 book ai didi

ios - 使用 GoogleMaps 时,myLocationEnabled 在 swift 上更改为 isMyLocationEnabled

转载 作者:行者123 更新时间:2023-11-28 06:29:25 24 4
gpt4 key购买 nike

我正在尝试获取用户当前位置以使用 google maps API 显示。数周以来我一直在为这个问题苦苦挣扎,当我在模拟器中更改位置时,代码运行但不会更新到当前位置。

我的代码在下面,在查看其他人的操作方式后,他们都有 mapView.myLocationEnabled = true,而对我来说,这会产生一个错误,提示“myLocationEnabled 已重命名为”isMylocationEnabled。我看到了一些最新的帖子(从 2 个月前开始)并且每个人都在使用 myLocationEnabled 似乎没有错误,为什么我得到这个?这可能是我当前位置未更新的原因吗?

import UIKit
import GoogleMaps

class FirstViewController: UIViewController, CLLocationManagerDelegate {


@IBOutlet weak var mapView: GMSMapView!


let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
mapView.settings.myLocationButton = true
mapView.settings.compassButton = true
}
override func viewDidAppear(_ animated: Bool) {
locationManager.requestWhenInUseAuthorization()
}
func locationManager(manager: CLLocationManager , didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()


mapView.isMyLocationEnabled = 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)

let marker = GMSMarker()
marker.title = "Current Location"
marker.map = mapView
locationManager.stopUpdatingLocation()
}
}
}

最佳答案

所以我终于解决了这个问题!基本上,我用 GoogleMaps 和 GooglePlaces 更新了我现有的 Podfile,然后更新了 pod 以确保所有框架都正常工作。然后我使用这段代码让我的当前位置工作

    import UIKit

import GoogleMaps


class FirstViewController: UIViewController, CLLocationManagerDelegate {


@IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()


override func viewDidLoad() {
super.viewDidLoad()
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
vwGMap.camera = camera

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 500
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

self.view = vwGMap
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

if (status == CLAuthorizationStatus.authorizedWhenInUse)

{
vwGMap.isMyLocationEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
vwGMap.settings.myLocationButton = true
self.view = self.vwGMap
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
marker.map = self.vwGMap
}

关于ios - 使用 GoogleMaps 时,myLocationEnabled 在 swift 上更改为 isMyLocationEnabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40792362/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com