开发者

iphone image not rotating properly aacording to location upon camera view

i am developing application which point towards the particular location, i have calculate angle between current coordinate to specific coordinate and my image is rotating by that difference but image rotation is fixed means when i change my direction the image is not moved it remain fix to specific postion, i want that the image should be moved aacording to the actual position where it is located(where we w开发者_StackOverflow社区ant to reach) thanks for the help in advance below is my code..

-(float) angleToRadians:(float) a {
    return ((a/180)*M_PI);
}
- (float) getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc
{

    float fLat = [self angleToRadians:fromLoc.latitude];
    float fLng = [self angleToRadians:fromLoc.longitude];
    float tLat = [self angleToRadians:toLoc.latitude];
    float tLng = [self angleToRadians:toLoc.longitude];
    float result= atan2f(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)); 

    return RadiansToDegrees(result);
}

- (void) scanButtonTouchUpInside {
    UIImage *overlayGraphic = [UIImage imageNamed:@"GFI.jpg"];
        overlayGraphicView = [[UIImageView alloc] initWithImage:overlayGraphic];
overlayGraphicView.frame = CGRectMake(50, 50, 50, 80);
        [self addSubview:overlayGraphicView];

    float angle =[self getHeadingForDirectionFromCoordinate:currentlocation  toCoordinate:pointlocation];
    if(angle < 0.0)
        angle += 2*M_PI;
overlayGraphicView.transform=CGAffineTransformMakeRotation(angle);


}


Judging by the name of the method that is rotating the image: scanButtonTouchUpInside, I'm guessing that it is called only when a user hits a button. If it is only called once, I would expect the image to only rotate once, then remain fixed. In order to keep the image rotating, you need to put that code somewhere that is called repeatedly. A (cheap hack) way to test this would be to add this line to the end of your - (void)scanButtonTouchUpInside method:

[self performSelector:@selector(scanButtonTouchUpInside) withObject:nil afterDelay:0.25f];

This will make the method call fire repeatedly. One thing to note though: It seems that your calculation of the angle of rotation depends on a variable called currentLocation. This solution will only work if you are updating currentLocation appropriately.

EDIT: Just a thought, you're currently calculating the angle of rotation based on the location of the device and the location of the destination. You're not taking into account compass bearing at all. Is this intentional? With this approach, your angle will never change unless the device actually moves. Any change in orientation will be ignored.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜