我要求在当前位置显示一个视图。如果设备旋转或位置会发生变化,它会旋转。我研究了很多但得到的所有代码在某个位置都有固定位置或角度,但我还没有修复位置。任何人都可以把我推向正确的方向。
我还使用了GMSMarker的旋转属性,但它无效。
- (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,因为没有转换方法。
如何在谷歌地图中旋转他们的汽车?
答案 0 :(得分:7)
我们可以根据课程属性CLLocation Class
旋转图像basic_string<CharT,Alloc>::basic_string( const basic_string & other );
答案 1 :(得分:4)
当前位置&#39; CLLocation&#39;对象有一个名为&#39; course&#39;
@property(readonly, nonatomic) CLLocationDirection course;
类型为CLLocationDirection(typedef为double),它是位置的角度。
要使汽车旋转,您需要在后端,方向以及纬度和经度上增加额外的场。使用此信息通过在UIView上应用Transform来旋转汽车
CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);
答案 2 :(得分:4)
你可以做类似的事情 -
-(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;
}
答案 3 :(得分:1)
//只做这个
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
double heading = newHeading.trueHeading;
marker.groundAnchor = CGPointMake(0.5, 0.5);
marker.rotation = heading;
marker.map = mapView;
}
答案 4 :(得分:1)
尝试一下,它对我有用
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()
}
答案 5 :(得分:0)
marker.rotation = course_of_location;
注意:只有在移动过程中才会旋转到针脚。在静态设备的情况下,location.course将有-1值。这也与设备方向无关。如果您希望根据设备标题移动引脚,请执行此操作
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
marker.rotation = (manager.heading.trueHeading) * M_PI / 180.0f; }
答案 6 :(得分:0)
这里的代码根据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;
}
}
答案 7 :(得分:0)
快速4 +
感谢priya.vr。更新位置标记时,请尝试以下操作:
let locationManager = CLLocationManager()
marker.position = position
marker.appearAnimation = .none
marker.rotation = locationManager.location?.course ?? 0
marker.map = map