Rotate GMSMarker in direction at which user facing












16














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 which have a fix location or angle at some location but i haven't fix location. Can anyone drive me at right direction.



I also used rotation property of the GMSMarker but it is not working.



- (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: I have rotation method but is there any way to rotate the GMSMarker because there is no method for transform.




How uber rotate their car in google map?




enter image description here










share|improve this question
























  • degree should be some variable you stored for previous heading degree.
    – zcui93
    Apr 6 '16 at 12:13










  • @zcui93 but at the first time how i find that degree
    – chirag shah
    Apr 6 '16 at 12:29










  • You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
    – zcui93
    Apr 6 '16 at 12:56










  • @chiragshah, jo baka thodi taklif to revani.. :)
    – Hemang
    Apr 11 '16 at 11:49










  • @Hemang please tame taklif thodi dur karo ne
    – chirag shah
    Apr 11 '16 at 11:57
















16














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 which have a fix location or angle at some location but i haven't fix location. Can anyone drive me at right direction.



I also used rotation property of the GMSMarker but it is not working.



- (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: I have rotation method but is there any way to rotate the GMSMarker because there is no method for transform.




How uber rotate their car in google map?




enter image description here










share|improve this question
























  • degree should be some variable you stored for previous heading degree.
    – zcui93
    Apr 6 '16 at 12:13










  • @zcui93 but at the first time how i find that degree
    – chirag shah
    Apr 6 '16 at 12:29










  • You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
    – zcui93
    Apr 6 '16 at 12:56










  • @chiragshah, jo baka thodi taklif to revani.. :)
    – Hemang
    Apr 11 '16 at 11:49










  • @Hemang please tame taklif thodi dur karo ne
    – chirag shah
    Apr 11 '16 at 11:57














16












16








16


6





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 which have a fix location or angle at some location but i haven't fix location. Can anyone drive me at right direction.



I also used rotation property of the GMSMarker but it is not working.



- (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: I have rotation method but is there any way to rotate the GMSMarker because there is no method for transform.




How uber rotate their car in google map?




enter image description here










share|improve this question















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 which have a fix location or angle at some location but i haven't fix location. Can anyone drive me at right direction.



I also used rotation property of the GMSMarker but it is not working.



- (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: I have rotation method but is there any way to rotate the GMSMarker because there is no method for transform.




How uber rotate their car in google map?




enter image description here







ios objective-c google-maps-markers cllocationmanager gmsmapview






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '16 at 5:31

























asked Apr 6 '16 at 12:00









chirag shah

1,8671439




1,8671439












  • degree should be some variable you stored for previous heading degree.
    – zcui93
    Apr 6 '16 at 12:13










  • @zcui93 but at the first time how i find that degree
    – chirag shah
    Apr 6 '16 at 12:29










  • You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
    – zcui93
    Apr 6 '16 at 12:56










  • @chiragshah, jo baka thodi taklif to revani.. :)
    – Hemang
    Apr 11 '16 at 11:49










  • @Hemang please tame taklif thodi dur karo ne
    – chirag shah
    Apr 11 '16 at 11:57


















  • degree should be some variable you stored for previous heading degree.
    – zcui93
    Apr 6 '16 at 12:13










  • @zcui93 but at the first time how i find that degree
    – chirag shah
    Apr 6 '16 at 12:29










  • You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
    – zcui93
    Apr 6 '16 at 12:56










  • @chiragshah, jo baka thodi taklif to revani.. :)
    – Hemang
    Apr 11 '16 at 11:49










  • @Hemang please tame taklif thodi dur karo ne
    – chirag shah
    Apr 11 '16 at 11:57
















degree should be some variable you stored for previous heading degree.
– zcui93
Apr 6 '16 at 12:13




degree should be some variable you stored for previous heading degree.
– zcui93
Apr 6 '16 at 12:13












@zcui93 but at the first time how i find that degree
– chirag shah
Apr 6 '16 at 12:29




@zcui93 but at the first time how i find that degree
– chirag shah
Apr 6 '16 at 12:29












You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
– zcui93
Apr 6 '16 at 12:56




You just assign it with a default value, corresponding to the default direction of your arrow when loaded.
– zcui93
Apr 6 '16 at 12:56












@chiragshah, jo baka thodi taklif to revani.. :)
– Hemang
Apr 11 '16 at 11:49




@chiragshah, jo baka thodi taklif to revani.. :)
– Hemang
Apr 11 '16 at 11:49












@Hemang please tame taklif thodi dur karo ne
– chirag shah
Apr 11 '16 at 11:57




@Hemang please tame taklif thodi dur karo ne
– chirag shah
Apr 11 '16 at 11:57












7 Answers
7






active

oldest

votes


















4














Current location 'CLLocation' object has a property called 'course'



@property(readonly, nonatomic) CLLocationDirection course;


of type CLLocationDirection(typedef of double) which is an angle of the location.



For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView



CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);





share|improve this answer























  • I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
    – chirag shah
    Apr 18 '16 at 5:21










  • @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
    – Uma Madhavi
    Mar 28 '17 at 11:45










  • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
    – Sunil_Vaishnav
    Mar 29 '17 at 9:04










  • Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
    – siva krishna
    Apr 25 '17 at 8:36





















6














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





share|improve this answer























  • It will be work fine
    – priya.vr
    Jan 5 '17 at 9:02



















4














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





share|improve this answer





















  • i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
    – ios developer
    Dec 8 '16 at 6:40










  • @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
    – Uma Madhavi
    Dec 15 '16 at 11:36



















1














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





share|improve this answer































    0














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





    share|improve this answer























    • Please explain the answer, why it will work?
      – rptwsthi
      Aug 20 '16 at 7:22






    • 2




      Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
      – Spydy
      Aug 21 '16 at 8:45





















    0














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





    share|improve this answer































      0














      Here the code modified with the changes suggested by Oren Trutner and from me:



      You just need to pass old location and new location. By passing required data you will get rotation value which is in 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;
      }
      }





      share|improve this answer





















      • are you sure this code work perfectly?
        – chirag shah
        Nov 26 at 5:31






      • 1




        I have tested and implemented in my one of the app recently.
        – Anil Kukadeja
        Nov 26 at 6:52











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36450526%2frotate-gmsmarker-in-direction-at-which-user-facing%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Current location 'CLLocation' object has a property called 'course'



      @property(readonly, nonatomic) CLLocationDirection course;


      of type CLLocationDirection(typedef of double) which is an angle of the location.



      For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView



      CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);





      share|improve this answer























      • I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
        – chirag shah
        Apr 18 '16 at 5:21










      • @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
        – Uma Madhavi
        Mar 28 '17 at 11:45










      • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
        – Sunil_Vaishnav
        Mar 29 '17 at 9:04










      • Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
        – siva krishna
        Apr 25 '17 at 8:36


















      4














      Current location 'CLLocation' object has a property called 'course'



      @property(readonly, nonatomic) CLLocationDirection course;


      of type CLLocationDirection(typedef of double) which is an angle of the location.



      For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView



      CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);





      share|improve this answer























      • I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
        – chirag shah
        Apr 18 '16 at 5:21










      • @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
        – Uma Madhavi
        Mar 28 '17 at 11:45










      • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
        – Sunil_Vaishnav
        Mar 29 '17 at 9:04










      • Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
        – siva krishna
        Apr 25 '17 at 8:36
















      4












      4








      4






      Current location 'CLLocation' object has a property called 'course'



      @property(readonly, nonatomic) CLLocationDirection course;


      of type CLLocationDirection(typedef of double) which is an angle of the location.



      For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView



      CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);





      share|improve this answer














      Current location 'CLLocation' object has a property called 'course'



      @property(readonly, nonatomic) CLLocationDirection course;


      of type CLLocationDirection(typedef of double) which is an angle of the location.



      For car to rotate, You need extra field in your backend, direction, along with latitude and longitude. Use this information to rotate car by applying Transform on UIView



      CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 15 '16 at 6:40









      Uma Madhavi

      3,82552259




      3,82552259










      answered Apr 16 '16 at 10:50









      Sunil_Vaishnav

      303415




      303415












      • I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
        – chirag shah
        Apr 18 '16 at 5:21










      • @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
        – Uma Madhavi
        Mar 28 '17 at 11:45










      • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
        – Sunil_Vaishnav
        Mar 29 '17 at 9:04










      • Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
        – siva krishna
        Apr 25 '17 at 8:36




















      • I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
        – chirag shah
        Apr 18 '16 at 5:21










      • @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
        – Uma Madhavi
        Mar 28 '17 at 11:45










      • CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
        – Sunil_Vaishnav
        Mar 29 '17 at 9:04










      • Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
        – siva krishna
        Apr 25 '17 at 8:36


















      I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
      – chirag shah
      Apr 18 '16 at 5:21




      I already try this but still you not provide me the answer where i put this "CGAffineTransformMakeRotation(M_PI * (course_of_location) / 180.0);" code. Because marker has not any property for transform.
      – chirag shah
      Apr 18 '16 at 5:21












      @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
      – Uma Madhavi
      Mar 28 '17 at 11:45




      @Sunil. how to set this CGAffineTransformMakeRotation in swift 3
      – Uma Madhavi
      Mar 28 '17 at 11:45












      CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
      – Sunil_Vaishnav
      Mar 29 '17 at 9:04




      CGAffineTransform(rotationAngle: (M_PI * (course_of_location) / 180.0))
      – Sunil_Vaishnav
      Mar 29 '17 at 9:04












      Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
      – siva krishna
      Apr 25 '17 at 8:36






      Hi I'm using course of location. But i don't want to turn the marker, I want to turn the mapview so that my marker always points to north. for this Im doing like this . mapView.animate(toBearing: CLLocationDirection.abs(LatestLocation.course)) But mais roating toomuch, Bcoz when user is turning left my course is 270. the same Im giving it to map to rotate, But want to rotate -90. how to do that
      – siva krishna
      Apr 25 '17 at 8:36















      6














      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





      share|improve this answer























      • It will be work fine
        – priya.vr
        Jan 5 '17 at 9:02
















      6














      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





      share|improve this answer























      • It will be work fine
        – priya.vr
        Jan 5 '17 at 9:02














      6












      6








      6






      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





      share|improve this answer














      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






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 5 '17 at 8:58

























      answered Jan 5 '17 at 8:53









      priya.vr

      119310




      119310












      • It will be work fine
        – priya.vr
        Jan 5 '17 at 9:02


















      • It will be work fine
        – priya.vr
        Jan 5 '17 at 9:02
















      It will be work fine
      – priya.vr
      Jan 5 '17 at 9:02




      It will be work fine
      – priya.vr
      Jan 5 '17 at 9:02











      4














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





      share|improve this answer





















      • i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
        – ios developer
        Dec 8 '16 at 6:40










      • @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
        – Uma Madhavi
        Dec 15 '16 at 11:36
















      4














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





      share|improve this answer





















      • i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
        – ios developer
        Dec 8 '16 at 6:40










      • @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
        – Uma Madhavi
        Dec 15 '16 at 11:36














      4












      4








      4






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





      share|improve this answer












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






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 25 '16 at 10:41









      Haider

      4,47921425




      4,47921425












      • i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
        – ios developer
        Dec 8 '16 at 6:40










      • @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
        – Uma Madhavi
        Dec 15 '16 at 11:36


















      • i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
        – ios developer
        Dec 8 '16 at 6:40










      • @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
        – Uma Madhavi
        Dec 15 '16 at 11:36
















      i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
      – ios developer
      Dec 8 '16 at 6:40




      i added this code. and it's work but one problem i have face is the marker is jump on that location and updated location. and also marker is continuously rotate even i don't move from my location.what can i do?
      – ios developer
      Dec 8 '16 at 6:40












      @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
      – Uma Madhavi
      Dec 15 '16 at 11:36




      @Haider. how can i set position of marker based on latitude & longitude .These are coming from server. How can i set rotation..
      – Uma Madhavi
      Dec 15 '16 at 11:36











      1














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





      share|improve this answer




























        1














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





        share|improve this answer


























          1












          1








          1






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





          share|improve this answer














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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 10 '16 at 15:55









          Tomasz Pluskiewicz

          2,95211033




          2,95211033










          answered Sep 10 '16 at 11:20









          ios meridian

          111




          111























              0














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





              share|improve this answer























              • Please explain the answer, why it will work?
                – rptwsthi
                Aug 20 '16 at 7:22






              • 2




                Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
                – Spydy
                Aug 21 '16 at 8:45


















              0














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





              share|improve this answer























              • Please explain the answer, why it will work?
                – rptwsthi
                Aug 20 '16 at 7:22






              • 2




                Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
                – Spydy
                Aug 21 '16 at 8:45
















              0












              0








              0






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





              share|improve this answer














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






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 21 '16 at 9:20

























              answered Aug 19 '16 at 6:36









              Spydy

              933910




              933910












              • Please explain the answer, why it will work?
                – rptwsthi
                Aug 20 '16 at 7:22






              • 2




                Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
                – Spydy
                Aug 21 '16 at 8:45




















              • Please explain the answer, why it will work?
                – rptwsthi
                Aug 20 '16 at 7:22






              • 2




                Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
                – Spydy
                Aug 21 '16 at 8:45


















              Please explain the answer, why it will work?
              – rptwsthi
              Aug 20 '16 at 7:22




              Please explain the answer, why it will work?
              – rptwsthi
              Aug 20 '16 at 7:22




              2




              2




              Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
              – Spydy
              Aug 21 '16 at 8:45






              Rotation is property of GMSMarker, CLLocation class provides the Course which actual direction of movement (not a direction of device). So if you set above code even you can simply do 'marker.rotation = location.course; it wil work when you will move. When there is no movement then course value is -1 and it will not show any rotation.
              – Spydy
              Aug 21 '16 at 8:45













              0














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





              share|improve this answer




























                0














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





                share|improve this answer


























                  0












                  0








                  0






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





                  share|improve this answer














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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 at 9:05









                  Rohan Kumar

                  35k95990




                  35k95990










                  answered Nov 12 at 8:43









                  BizDev

                  88111




                  88111























                      0














                      Here the code modified with the changes suggested by Oren Trutner and from me:



                      You just need to pass old location and new location. By passing required data you will get rotation value which is in 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;
                      }
                      }





                      share|improve this answer





















                      • are you sure this code work perfectly?
                        – chirag shah
                        Nov 26 at 5:31






                      • 1




                        I have tested and implemented in my one of the app recently.
                        – Anil Kukadeja
                        Nov 26 at 6:52
















                      0














                      Here the code modified with the changes suggested by Oren Trutner and from me:



                      You just need to pass old location and new location. By passing required data you will get rotation value which is in 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;
                      }
                      }





                      share|improve this answer





















                      • are you sure this code work perfectly?
                        – chirag shah
                        Nov 26 at 5:31






                      • 1




                        I have tested and implemented in my one of the app recently.
                        – Anil Kukadeja
                        Nov 26 at 6:52














                      0












                      0








                      0






                      Here the code modified with the changes suggested by Oren Trutner and from me:



                      You just need to pass old location and new location. By passing required data you will get rotation value which is in 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;
                      }
                      }





                      share|improve this answer












                      Here the code modified with the changes suggested by Oren Trutner and from me:



                      You just need to pass old location and new location. By passing required data you will get rotation value which is in 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;
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 24 at 10:41









                      Anil Kukadeja

                      7711923




                      7711923












                      • are you sure this code work perfectly?
                        – chirag shah
                        Nov 26 at 5:31






                      • 1




                        I have tested and implemented in my one of the app recently.
                        – Anil Kukadeja
                        Nov 26 at 6:52


















                      • are you sure this code work perfectly?
                        – chirag shah
                        Nov 26 at 5:31






                      • 1




                        I have tested and implemented in my one of the app recently.
                        – Anil Kukadeja
                        Nov 26 at 6:52
















                      are you sure this code work perfectly?
                      – chirag shah
                      Nov 26 at 5:31




                      are you sure this code work perfectly?
                      – chirag shah
                      Nov 26 at 5:31




                      1




                      1




                      I have tested and implemented in my one of the app recently.
                      – Anil Kukadeja
                      Nov 26 at 6:52




                      I have tested and implemented in my one of the app recently.
                      – Anil Kukadeja
                      Nov 26 at 6:52


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f36450526%2frotate-gmsmarker-in-direction-at-which-user-facing%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues