Rotate GMSMarker in direction at which user facing

前端 未结 8 1172
独厮守ぢ
独厮守ぢ 2020-12-28 17:58

I have requirement like on my current location one view will display. It will rotate if device was rotate or location will be change.I research lot but got all the code whic

相关标签:
8条回答
  • 2020-12-28 18:37

    Try this, it worked for me

    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 讨论(0)
  • 2020-12-28 18:43
    marker.rotation = course_of_location;
    

    Note: rotation to pin will happen only during movement. in case of static device there will be -1 value of location.course. Also this have no relation with device orientation. If you are looking to move pin according to device heading then do this

    - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    marker.rotation =  (manager.heading.trueHeading) * M_PI / 180.0f; }
    
    0 讨论(0)
  • 2020-12-28 18:44

    We can rotate image based on course property 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 
    
    0 讨论(0)
  • 2020-12-28 18:45

    //just do this only

    - (void)locationManager:(CLLocationManager *)manager  didUpdateHeading:(CLHeading *)newHeading
    {
       double heading = newHeading.trueHeading;
       marker.groundAnchor = CGPointMake(0.5, 0.5);
       marker.rotation = heading;
       marker.map = mapView;
    }
    
    0 讨论(0)
  • 2020-12-28 18:48

    Swift 4+

    Thanks to priya.vr. When you updating location marker, try this:

     let locationManager = CLLocationManager()
     marker.position = position
     marker.appearAnimation = .none
     marker.rotation = locationManager.location?.course ?? 0
     marker.map = map
    
    0 讨论(0)
  • 2020-12-28 18:52

    you can do something like -

    -(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;
    }
    
    0 讨论(0)
提交回复
热议问题