像 Uber iOS 应用程序一样在地图上移动注释

Move the annotation on Map like Uber iOS application

我正在做一个项目,我需要创建一个与 UBER 和 OLA 类似的 iOS 应用程序,其中汽车根据位置移动。我正在寻找某种可以让汽车像 OLA 一样平稳移动和转弯的库。现在,我能够将汽车从一个纬度-经度移动到另一个纬度-经度。但比较复杂的是如何在转向的时候转弯,保证车子朝向前方。

请找到下面的截图。

其实我在之前的iOS申请中也有一个要求,所以请找到下面URL下载代码并查看它。

Environment: Xcode 11 and Swift 5

Link: https://github.com/ram2386/Track-Car

突出显示我完成它的代码。

要完成与 Uber iOS 应用程序相同的移动汽车功能,您需要首先计算旧位置和新位置之间的角度。请找到以下代码以了解如何计算它。

func angleFromCoordinate(firstCoordinate: CLLocationCoordinate2D, 
    secondCoordinate: CLLocationCoordinate2D) -> Double {
    
    let deltaLongitude: Double = secondCoordinate.longitude - firstCoordinate.longitude
    let deltaLatitude: Double = secondCoordinate.latitude - firstCoordinate.latitude
    let angle = (Double.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
    
    if (deltaLongitude > 0) {
        return angle
    } else if (deltaLongitude < 0) {
        return angle + Double.pi
    } else if (deltaLatitude < 0) {
        return Double.pi
    } else {
        return 0.0
    }
}

//Apply the angle to the particular annotation for moving
let getAngle = angleFromCoordinate(firstCoordinate: oldLocation, secondCoordinate: newLocation)

//Apply the new location for coordinate
myAnnotation.coordinate = newLocation;

//Getting the MKAnnotationView
let annotationView = self.mapView.view(for: myAnnotation)

//Angle for moving the car
annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(getAngle))

请找到下面的 GIF 表示,它在地图上的样子。

为了完成功能,我创建了 .csv 文件,其中添加了 1000 条纬度和经度记录。

注意:您根据位置获得角度,因此有时会发生由于位置而无法获得正确角度的情况,因为这完全取决于您的位置。

希望对你有用!!!