Swift 有没有可能从地图工具包中获取纬度和经度并在谷歌地图中使用?

Swift 有没有可能从地图工具包中获取纬度和经度并在谷歌地图中使用?,swift,xcode,Swift,Xcode,除了标题没什么可说的 代码: 但是结果不正确,它显示我离现在的位置很远,有什么帮助吗 我使用的是Xcode 10 swift 4,您将currentLat和currentLong都设置为经度: currentLat = (locationManager.location?.coordinate.longitude)! currentLong = (locationManager.location?.coordinate.longitude)! 应该是: currentLat = (locati

除了标题没什么可说的 代码:

但是结果不正确,它显示我离现在的位置很远,有什么帮助吗


我使用的是Xcode 10 swift 4,您将currentLat和currentLong都设置为经度:

currentLat = (locationManager.location?.coordinate.longitude)!
currentLong = (locationManager.location?.coordinate.longitude)!
应该是:

currentLat = (locationManager.location?.coordinate.latitude)!
currentLong = (locationManager.location?.coordinate.longitude)!
参考,您应该通过委托方法locationManager接收位置更新。\uDidUpdateLocations:

启动日期位置

此方法立即返回。调用此方法会导致位置管理器获得初始位置修复,这可能需要几秒钟的时间,并通过调用其locationManager\uUdidUpdateLocations:方法通知您的代理。之后,接收器主要在超过distanceFilter属性中的值时生成更新事件。不过,在其他情况下可能会提供更新。例如,如果硬件收集到更准确的位置读数,则接收器可以发送另一个通知

因此,您应该调用startUpdatingLocation并等待在接收到GPS位置时调用委托方法

因此,请尝试以下方法:

class onlyMapVC: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var theMap: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        theMap.delegate = self

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, 
           didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.first?.coordinate
        marker.map = theMap

        self.theMap.camera = GMSCameraPosition(target: currentLocation, zoom: 6.0, bearing: 0, viewingAngle: 0)

    }
}

确保UIViewController具有协议CLLocationManagerDelegate

currentLat,并且上述代码中不存在currentLong变量。当然,你可以在另一个地图SDK中使用坐标,坐标是通用的如果可能的话,为什么它们会显示我的另一个位置使用谷歌地图获取我当前位置的最佳方式是什么,完全没有注意到我自己我修复了它,但到目前为止仍然显示位置不是我的:当您在viewDidLoad中访问位置时,可能locationmanager没有时间更新位置?正如@Scriptable所写的,您应该使用委托方法来获取更新的位置。顺便说一句,您是在设备上还是在模拟器中尝试此操作?在我自己的iPhone X上
class onlyMapVC: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var theMap: GMSMapView!

    override func viewDidLoad() {
        super.viewDidLoad()

        theMap.delegate = self

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    func locationManager(_ manager: CLLocationManager, 
           didUpdateLocations locations: [CLLocation]) {
        let currentLocation = locations.first?.coordinate
        marker.map = theMap

        self.theMap.camera = GMSCameraPosition(target: currentLocation, zoom: 6.0, bearing: 0, viewingAngle: 0)

    }
}