【问题标题】:How to navigate vehicle icon for current location using google map SDK in swift?如何在 swift 中使用谷歌地图 SDK 导航当前位置的车辆图标?
【发布时间】:2020-02-18 16:53:00
【问题描述】:

我正在调用方向 API 来绘制路线并尝试在用户移动位置时移动当前位置标记图标,但我在实现时面临以下问题。

  • 移动图标时,图标好像倾斜了。
  • 它不显示路线上的实际用户位置,图标看起来像是在路线的一侧行驶。
  • 我在谷歌地图上的位置图标(蓝色图标)不断改变其位置。
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    let direction = newHeading.trueHeading
        lastDriverAngleFromNorth = direction
        self.sourceMarker?.rotation = (lastDriverAngleFromNorth - mapBearing) - bearingValue

        DispatchQueue.main.async {
            CATransaction.begin()
            CATransaction.setValue(2, forKey: kCATransactionAnimationDuration)
                self.gmsmapView?.animate(toBearing: newHeading.magneticHeading)
                    CATransaction.commit()
    }
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let lastLocation = locations.last {

        self.currentLocation = lastLocation
            let zoom = self.gmsmapView?.camera.zoom ?? 20.0
            zoomLevel = zoom
            let destination = CLLocation.init(latitude: viewModel.marker.location[0], longitude: viewModel.marker.location[1])
            let bearing = getBearingBetweenTwoPoints(point1: lastLocation.coordinate, point2: destination.coordinate)
            self.cameraMoveToLocation(toLocation: lastLocation, zoom:zoom, bearing: bearing)
    }
}

func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
    mapBearing  = position.bearing
    if let coordinate = self.currentLocation?.coordinate{
            self.rerouteCalculation(currentLocation:coordinate)
    }

    self.sourceMarker?.rotation = (lastDriverAngleFromNorth - mapBearing) - bearingValue
}

func cameraMoveToLocation(toLocation: CLLocation, zoom : Float, bearing : Double) {
    self.gmsmapView?.animate(toLocation: toLocation.coordinate)
    self.sourceMarker?.position = toLocation.coordinate
}

谁能帮帮我,我被困在这里了。

【问题讨论】:

  • 你有没有尝试到外面走走,看清楚天空?这听起来像是 GPS 精度问题。
  • 位置标记周围的透明蓝色圆圈是当前 GPS 精度误差。你的位置标记在这个区域跳来跳去是正常的。
  • 是的,我试过了,但我主要关心的是如何在路线上顺畅地导航车辆图标,包括所有转弯和地图方位

标签: ios swift google-maps google-directions-api


【解决方案1】:

确保根据需要选择正确的位置精度。
CCLocationAccuracy 提供以下选项:

  • kCLLocationAccuracyBestForNavigation
    使用额外传感器数据的最高可能精度 方便导航应用程序。

  • kCLLocationAccuracyBest
    可用的最佳精度水平。

  • kCLLocationAccuracyNearestTenMeters:准确 距离目标目标十米以内。

  • kCLLocationAccuracyHundredMeters:准确到 百米以内。

  • kCLLocationAccuracyKilometer:准确到 最近的公里数。

  • kCLLocationAccuracyThreeKilometers:准确 到最近的三公里。

【讨论】:

    猜你喜欢
    • 2016-04-29
    • 1970-01-01
    • 1970-01-01
    • 2013-09-03
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多