在面向用户的方向上旋转GMSMarker

问题描述 投票:16回答:8

我要求在我当前的位置显示一个视图。如果设备旋转或位置将改变它将旋转。我研究很多,但得到所有代码,在某些位置有固定位置或角度但我没有固定位置。任何人都可以朝正确的方向开车。

我还使用了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,因为没有转换方法。

如何在谷歌地图上旋转他们的车?

enter image description here

ios objective-c google-maps-markers cllocationmanager gmsmapview
8个回答
5
投票

当前位置'CLLocation'对象有一个名为'course'的属性

@property(readonly, nonatomic) CLLocationDirection course;

类型为CLLocationDirection(typedef为double),它是位置的角度。

要使汽车旋转,您需要在后端,方向以及纬度和经度上增加额外的场。使用此信息通过在UIView上应用Transform来旋转汽车

CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);

7
投票

我们可以根据课程属性CLLocation Class旋转图像

    let marker:GMSMarker = GMSMarker.init(position: currentLocation!)
    let head = locationManager.location?.course ?? 0
    marker.rotation = head
    marker.icon = UIImage(named: "testyCar.png")
    marker.map = mapView 

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;
}

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;
}

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()
}

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; }

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;
    }
}

0
投票

Swift 4+

感谢priya.vr。更新位置标记时,请尝试以下操作:

 let locationManager = CLLocationManager()
 marker.position = position
 marker.appearAnimation = .none
 marker.rotation = locationManager.location?.course ?? 0
 marker.map = map
© www.soinside.com 2019 - 2024. All rights reserved.