沿用户面向的方向旋转 GMSMarker
Rotate GMSMarker in direction at which user facing
我有一个要求,比如在我当前的位置会显示一个视图。如果设备旋转或位置将 change.I 研究很多,它会旋转,但获得了在某个位置具有固定位置或角度的所有代码,但我没有固定位置。谁能带我去正确的方向。
我还使用了 GMSMarker 的 rotation 属性,但它不起作用。
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if (newHeading.headingAccuracy < 0){
NSLog(@"heading accuracy < 0");
return;
}
// Convert Degree to Radian and move the needle
float oldRad = (- manager.heading.trueHeading) * M_PI / 180.0f;
float newRad = (- newHeading.trueHeading) * M_PI / 180.0f;
// Compass animation
CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue = [NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.5f;
[source.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
// source.transform = CGAffineTransformMakeRotation(newRad)
// GMSMarker *source = [googleMap selectedMarker];
// source.rotation = newRad;
}
更新:我有旋转方法,但是有什么方法可以旋转 GMSMarker,因为没有变换方法。
How uber rotate their car in google map?
当前位置 'CLLocation' 对象有一个 属性 称为 'course'
@property(readonly, nonatomic) CLLocationDirection course;
类型为 CLLocationDirection(typedef of double) 的位置角度。
为了使汽车旋转,您需要在后端、方向以及纬度和经度中添加额外的字段。使用此信息通过在 UIView 上应用变换来旋转汽车
CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);
你可以这样做 -
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
CLLocationDirection direction = newHeading.trueHeading;
lastDriverAngleFromNorth = direction;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
mapBearing = position.bearing;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}
marker.rotation = course_of_location;
注意:只有在移动过程中才会旋转到图钉。如果是静态设备,location.course 的值为 -1。这也与设备方向无关。如果您想根据设备标题移动引脚,请执行此操作
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
marker.rotation = (manager.heading.trueHeading) * M_PI / 180.0f; }
//只做这个
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double heading = newHeading.trueHeading;
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.rotation = heading;
marker.map = mapView;
}
我们可以根据课程属性 CLLocation Class
旋转图像
let marker = GMSMarker(position: currentLocation!)
let head = locationManager.location?.course ?? 0
marker.rotation = head
marker.icon = UIImage(named: "testyCar.png")
marker.map = mapView
试试这个,对我有用
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading:CLHeading) {
UIView.animate(withDuration: 0.005) {
let angle = newHeading.trueHeading.toRadians() // convert from degrees to radians
self.yourImageHere.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) // rotate the picture
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingHeading()
}
这里是根据 Oren Trutner 和我建议的更改修改的代码:
您只需传递旧位置和新位置即可。通过传递所需的数据,您将获得浮动的旋转值。
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (x * 180.0 / M_PI)
- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);
float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));
if (degree >= 0) {
return degree;
} else {
return 360+degree;
}
}
Swift 4+
感谢priya.vr。当您更新位置标记时,试试这个:
let locationManager = CLLocationManager()
marker.position = position
marker.appearAnimation = .none
marker.rotation = locationManager.location?.course ?? 0
marker.map = map
我有一个要求,比如在我当前的位置会显示一个视图。如果设备旋转或位置将 change.I 研究很多,它会旋转,但获得了在某个位置具有固定位置或角度的所有代码,但我没有固定位置。谁能带我去正确的方向。
我还使用了 GMSMarker 的 rotation 属性,但它不起作用。
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
if (newHeading.headingAccuracy < 0){
NSLog(@"heading accuracy < 0");
return;
}
// Convert Degree to Radian and move the needle
float oldRad = (- manager.heading.trueHeading) * M_PI / 180.0f;
float newRad = (- newHeading.trueHeading) * M_PI / 180.0f;
// Compass animation
CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue = [NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.5f;
[source.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
// source.transform = CGAffineTransformMakeRotation(newRad)
// GMSMarker *source = [googleMap selectedMarker];
// source.rotation = newRad;
}
更新:我有旋转方法,但是有什么方法可以旋转 GMSMarker,因为没有变换方法。
How uber rotate their car in google map?
当前位置 'CLLocation' 对象有一个 属性 称为 'course'
@property(readonly, nonatomic) CLLocationDirection course;
类型为 CLLocationDirection(typedef of double) 的位置角度。
为了使汽车旋转,您需要在后端、方向以及纬度和经度中添加额外的字段。使用此信息通过在 UIView 上应用变换来旋转汽车
CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);
你可以这样做 -
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
CLLocationDirection direction = newHeading.trueHeading;
lastDriverAngleFromNorth = direction;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}
#pragma mark - GMSMapViewDelegate
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
mapBearing = position.bearing;
self.driverMarker.rotation = lastDriverAngleFromNorth - mapBearing;
}
marker.rotation = course_of_location;
注意:只有在移动过程中才会旋转到图钉。如果是静态设备,location.course 的值为 -1。这也与设备方向无关。如果您想根据设备标题移动引脚,请执行此操作
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
marker.rotation = (manager.heading.trueHeading) * M_PI / 180.0f; }
//只做这个
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double heading = newHeading.trueHeading;
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.rotation = heading;
marker.map = mapView;
}
我们可以根据课程属性 CLLocation Class
旋转图像 let marker = GMSMarker(position: currentLocation!)
let head = locationManager.location?.course ?? 0
marker.rotation = head
marker.icon = UIImage(named: "testyCar.png")
marker.map = mapView
试试这个,对我有用
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading:CLHeading) {
UIView.animate(withDuration: 0.005) {
let angle = newHeading.trueHeading.toRadians() // convert from degrees to radians
self.yourImageHere.transform = CGAffineTransform(rotationAngle: CGFloat(angle)) // rotate the picture
}
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.startUpdatingHeading()
}
这里是根据 Oren Trutner 和我建议的更改修改的代码:
您只需传递旧位置和新位置即可。通过传递所需的数据,您将获得浮动的旋转值。
#define degreesToRadians(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (x * 180.0 / M_PI)
- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{
float fLat = degreesToRadians(fromLoc.latitude);
float fLng = degreesToRadians(fromLoc.longitude);
float tLat = degreesToRadians(toLoc.latitude);
float tLng = degreesToRadians(toLoc.longitude);
float degree = radiansToDegrees(atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)));
if (degree >= 0) {
return degree;
} else {
return 360+degree;
}
}
Swift 4+
感谢priya.vr。当您更新位置标记时,试试这个:
let locationManager = CLLocationManager()
marker.position = position
marker.appearAnimation = .none
marker.rotation = locationManager.location?.course ?? 0
marker.map = map