问题描述
我正在使用iOS版Google Maps SDK.
I'm working with Google Maps SDK for iOS.
我的项目需要能够沿绘制在地图视图上的预定路径将当前位置标记移动x米.因此,我已经从这个问题(Koray的答案)中实现了以下方法Birand):
My project requires the ability of moving the current location marker by x meters along the pre-determined path drawn on to the map view. Therefore I've implemented the following method from this question(the answer by Koray Birand):
func distance(distance: Double, AwayFromCoordinate origin: CLLocationCoordinate2D, WithBearing bearing: Double) -> CLLocationCoordinate2D {
let earthRadius = 6372797.6 // in meters
let radianDistance = distance / earthRadius
let radianBearing = bearing * M_PI / 180.0
let latitude = origin.latitude * M_PI / 180
let longitude = origin.longitude * M_PI / 180
let lat2 = asin(sin(latitude) * cos(radianDistance) + cos(latitude) * sin(radianDistance) * cos(radianBearing))
let lon2 = longitude + atan2(sin(radianBearing) * sin(radianDistance) * cos(latitude), cos(radianDistance) - sin(longitude) * sin(lat2))
return CLLocationCoordinate2DMake(lat2 * 180 / M_PI, lon2 * 180 / M_PI)
}
因此,如果我像下面这样调用上述方法:
So if I call the above method like the following:
currentLocationMarker.position = distance(0.5, AwayFromCoordinate: currentLocationMarker.position, WithBearing: bearing)
这为我提供了一个距离当前位置0.5米的新位置.
This yields me a new location 0.5 meters away from the current location.
但是,即使起始点相同,机芯也会开始出现如下所示的轻微偏移:
However there is a slight problem that the movement starts to deflect a little like below even though the starting point is the same:
我不知道为什么会发生这种偏转而不是直行.有什么想法吗?
I couldn't figure out why such a deflection happens and not goes straight. Any ideas?
推荐答案
我相信您从 this answer获得了此解决方案,对吧?如果是这样,请注意,在计算lon2
时,您要做的是:
I believe you got this solution from this answer, right?If so, notice that when calculating the lon2
you do:
let lon2 = longitude + atan2(sin(radianBearing) * sin(radianDistance) * cos(latitude),
cos(radianDistance) - sin(longitude) * sin(lat2))
现在看一下原始答案的代码:
now take a look at the original answer's code:
let lon2 = lon1 + atan2(sin(rbearing) * sin(distRadians) * cos(lat1),
cos(distRadians) - sin(lat1) * sin(lat2))
然后注意最后一点:...- sin(lat1) * sin(lat2))
在您的代码中不是纬度,而是使用一个纬度和一个经度:...- sin(longitude) * sin(lat2))
And notice the last bit: ...- sin(lat1) * sin(lat2))
which in your code instead of latitudes uses one latitude and one longitude: ...- sin(longitude) * sin(lat2))
所以正确的解决方案可能是:
So probably the right solution would be:
let lon2 = longitude + atan2(sin(radianBearing) * sin(radianDistance) * cos(latitude),
cos(radianDistance) - sin(latitude) * sin(lat2))
也许这是问题的根源?
我希望它能帮助=)
这篇关于X米移动GMSMarker偏转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!