web-dev-qa-db-fra.com

Faire pivoter GMSMarker dans la direction vers laquelle l'utilisateur fait face

J'ai des exigences, comme sur mon emplacement actuel, une vue s'affichera. Il effectuera une rotation si l’appareil était en rotation ou si l’emplacement sera modifié.J’ai fait une recherche sur le lot, mais j’ai obtenu tous les codes qui ont un emplacement ou un angle fixe à un endroit donné, mais je n’ai pas déterminé l’emplacement. Quelqu'un peut-il me conduire dans la bonne direction?.

J'ai également utilisé la propriété rotation de GMSMarker, mais cela ne fonctionne pas.

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

Update: J'ai la méthode de rotation, mais existe-t-il un moyen de faire pivoter GMSMarker car il n'y a pas de méthode de transformation?.

Comment uber faire pivoter leur voiture dans google map?

 enter image description here

16
chirag shah

L'objet 'CLLocation' de l'emplacement actuel a une propriété appelée 'cours' 

@property(readonly, nonatomic) CLLocationDirection course;

de type CLLocationDirection (typedef of double) qui est un angle de l'emplacement. 

Pour que la voiture puisse pivoter, il vous faut un champ supplémentaire dans votre arrière-plan, votre direction, ainsi que votre latitude et votre longitude. Utilisez ces informations pour faire pivoter un véhicule en appliquant Transform sur UIView. 

CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);
4
Sunil_Vaishnav

Nous pouvons faire pivoter l'image en fonction de la propriété de cours 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 
6
priya.vr

vous pouvez faire quelque chose comme - 

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

// fais juste ça seulement 

- (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
ios meridian

Essayez ceci, cela a fonctionné pour moi

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

Swift 4+

Merci à priya.vr . Lorsque vous mettez à jour le marqueur de position, essayez ceci:

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

Voici le code modifié avec les modifications suggérées par Oren Trutner et de moi:

Vous devez juste passer l'ancien et le nouvel emplacement. En transmettant les données requises, vous obtiendrez une valeur de rotation qui est en float.

#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
Anil Kukadeja
marker.rotation = course_of_location;

Remarque: la rotation de la goupille ne se produira que pendant le mouvement. en cas de périphérique statique, il y aura -1 valeur de location.course. De plus, cela n’a aucun rapport avec l’orientation de l’appareil. Si vous souhaitez déplacer la broche en fonction du cap de l'appareil, faites-le

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