【问题标题】:Rotate GMSMarker in direction at which user facing沿用户面向的方向旋转 GMSMarker
【发布时间】:2016-07-26 19:41:30
【问题描述】:

我有一个要求,比如我当前的位置将显示一个视图。如果设备旋转或位置将改变,它将旋转。我研究了很多,但得到了所有在某个位置具有固定位置或角度但我没有固定位置的代码。谁能带我往正确的方向前进。

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

优步如何在谷歌地图中旋转他们的汽车?

【问题讨论】:

  • degree 应该是您为之前的航向度存储的一些变量。
  • @zcui93 但我第一次如何找到那个学位
  • 您只需为其分配一个默认值,对应于加载时箭头的默认方向。
  • @chiragshah, jo baka thodi taklif to revani.. :)
  • @Hemang 请驯服 taklif thodi dur karo ne

标签: ios objective-c google-maps-markers cllocationmanager gmsmapview


【解决方案1】:

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

@property(readonly, nonatomic) CLLocationDirection course;

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

为了让汽车旋转,您需要在后端、方向以及纬度和经度中添加额外的字段。使用此信息通过在 UIView 上应用 Transform 来旋转汽车

CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);

【讨论】:

  • 我已经尝试过了,但你仍然没有给我答案,我把这个 "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);"代码。因为marker没有任何变换属性。
  • @Sunil。如何在 swift 3 中设置此 CGAffineTransformMakeRotation
  • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
  • 您好,我正在使用位置课程。但我不想转动标记,我想转动地图视图,以便我的标记始终指向北方。为此,我正在这样做。 mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) 但是mais roating too much,Bcoz 当用户向左转时我的路线是270。我给它地图旋转,但想旋转-90。如何做到这一点
【解决方案2】:

你可以做类似的事情 -

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

【讨论】:

  • 我添加了这段代码。它可以工作,但我面临的一个问题是标记是跳转到该位置并更新位置。即使我没有从我的位置移动,标记也会不断旋转。我该怎么办?
  • @Haider。我如何根据纬度和经度设置标记的位置。这些来自服务器。我如何设置旋转..
【解决方案3】:
marker.rotation = course_of_location;

注意:旋转到销只会在移动过程中发生。如果是静态设备,location.course 的值为 -1。这也与设备方向无关。如果您想根据设备标题移动图钉,请执行此操作

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
marker.rotation =  (manager.heading.trueHeading) * M_PI / 180.0f; }

【讨论】:

  • Rotation 是 GMSMarker 的属性,CLLocation 类提供了实际运动方向(不是设备方向)的 Course。因此,如果您设置上面的代码,即使您可以简单地执行 'marker.rotation = location.course;当你移动时它会起作用。当没有运动时,航向值为-1,不会显示任何旋转。
【解决方案4】:

//只做这个

- (void)locationManager:(CLLocationManager *)manager  didUpdateHeading:(CLHeading *)newHeading
{
   double heading = newHeading.trueHeading;
   marker.groundAnchor = CGPointMake(0.5, 0.5);
   marker.rotation = heading;
   marker.map = mapView;
}

【讨论】:

    【解决方案5】:

    我们可以根据课程属性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 
    

    【讨论】:

      【解决方案6】:

      试试这个,它对我有用

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

      【讨论】:

        【解决方案7】:

        这里是使用 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;
            }
        }
        

        【讨论】:

        • 我最近在我的一款应用中测试并实现了。
        【解决方案8】:

        Swift 4+

        感谢priya.vr。当你更新位置标记时,试试这个:

         let locationManager = CLLocationManager()
         marker.position = position
         marker.appearAnimation = .none
         marker.rotation = locationManager.location?.course ?? 0
         marker.map = map
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-07
          • 1970-01-01
          • 2017-01-11
          • 1970-01-01
          • 1970-01-01
          • 2011-11-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多