Ram*_*rma 15 objective-c latitude-longitude mkmapview mkannotation xcode8
我正在开发一个项目,我需要为UBER和OLA创建类似的iOS应用程序,其中汽车根据位置移动.我正在寻找某种类型的图书馆,它可以让汽车像OLA一样顺畅地转动.现在我能够将汽车从一个纬度经度移动到另一个纬度经度.但复杂的部分是如何转向并确保汽车在朝向方向时面向前方.
请查看以下屏幕截图.
Ram*_*rma 48
实际上我在之前的iOS应用程序中也有一个要求,所以请找到下面的URL以下载代码并查看它.
iOS地图链接:https://www.dropbox.com/s/4jj8em1o786qa9j/TestAnnotationMoving.zip?dl=0
环境:Xcode 8和Objective-C
突出显示我已经完成的代码.
为了完成与Uber iOS应用程序相同的移动汽车功能,您需要首先计算旧位置和新位置之间的角度.请查看以下代码以了解如何计算它.
- (float)angleFromCoordinate:(CLLocationCoordinate2D)first
toCoordinate:(CLLocationCoordinate2D)second {
float deltaLongitude = second.longitude - first.longitude;
float deltaLatitude = second.latitude - first.latitude;
float angle = (M_PI * .5f) - atan(deltaLatitude / deltaLongitude);
if (deltaLongitude > 0) return angle;
else if (deltaLongitude < 0) return angle + M_PI;
else if (deltaLatitude < 0) return M_PI;
return 0.0f;
}
Run Code Online (Sandbox Code Playgroud)
然后将角度应用于特定注释以进行移动.请找到相同的以下代码.
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];
//Apply the new location for coordinate.
myAnnotation.coordinate = newLocation;
//For getting the MKAnnotationView.
AnnotationView *annotationView = (AnnotationView *)[self.mapView viewForAnnotation:myAnnotation];
//Apply the angle for moving the car.
annotationView.transform = CGAffineTransformMakeRotation(getAngle);
Run Code Online (Sandbox Code Playgroud)
对于谷歌地图
谷歌地图链接:https: //www.dropbox.com/s/a5ts108lh5rr1gd/GoogleMap.zip?dl=0
如何运行项目?
创建GMSMarker的对象.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(kCameraLatitude, kCameraLongitude);
marker.map = _mapView;
marker.icon = [UIImage imageNamed:@"carIcon"];
Run Code Online (Sandbox Code Playgroud)
获取两个位置之间的角度并应用它们来计算角度请使用上述功能.
//Get the angle
float getAngle = [self angleFromCoordinate:oldLocation toCoordinate:newLocation];
//Apply the new coordinate
marker.position = newLocation;
//Apply the angle and convert to degree from radian.
marker.rotation = getAngle * (180.0 / M_PI);
Run Code Online (Sandbox Code Playgroud)
如在苹果地图中.transform采用弧度但在谷歌地图中.rotation需要度数.
请在下面找到GIF表示,它在地图上的外观.
为了完成功能,我创建了.csv文件,其中我添加了1000个纬度和经度记录.
注意:您可以根据位置获得角度,因此有时您会因位置而无法获得正确的角度,因为它完全取决于您的位置.
希望这对你有用!!!