在Uber iOS应用程序上移动地图上的注释
问题描述:
我正在开发一个项目,在该项目中,我需要为UBER和OLA创建基于位置的汽车正在移动的类似iOS应用程序。我正在寻找某种类型的图书馆,它可以使汽车像OLA一样移动并顺利转弯。现在我能够将汽车从一个经纬度移动到另一个纬度。但是复杂的部分是如何在转向时确保汽车面向前方。在Uber iOS应用程序上移动地图上的注释
请查阅下面的截图。
答
其实我也有我以前的iOS应用的一个要求,所以请找到下面的网址下载的代码,并进行审查。
链接为iOS地图:https://www.dropbox.com/s/4jj8em1o786qa9j/TestAnnotationMoving.zip?dl=0
环境:8的Xcode和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;
}
然后将角度应用于移动的特定注释。请为下面的代码找到相同的代码。
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);
对于谷歌地图
的谷歌地图链接: https://www.dropbox.com/s/a5ts108lh5rr1gd/GoogleMap.zip?dl=0
如何运行项目?
- 从上面的项目下载dropbox链接。
- 从link获取MapsAPIKey。
- 运行cocoapods为iOS安装google map sdk。
创建GMSMarker的对象。
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(kCameraLatitude, kCameraLongitude);
marker.map = _mapView;
marker.icon = [UIImage imageNamed:@"carIcon"];
获取两个位置之间的角度并应用它们来计算角度请使用上面的函数。
//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);
正如苹果地图.transform采取弧度,但在谷歌地图.rotation需要的程度。
请在下面找到GIF表情,看它在地图上的样子。
为了完成我创造,我又增加了1000条记录经纬度的.csv文件的功能。
注意:您根据位置获得角度,因此有时会出现由于位置而无法获得正确角度的情况,因为它完全取决于您的位置。
希望它适合你!
感谢您的回答,我会检查并给出我的反馈。 –
当然,如果它适合你,那么请接受答案。 –
@RamkrishnaSharma加1解释这一点,我们可以用'gmsmapview'也可以吗? – vaibhav