【问题标题】:Animate marker in Google Maps SDK in iOSiOS 中 Google Maps SDK 中的动画标记
【发布时间】:2013-08-28 07:59:44
【问题描述】:

我在地图上有一个图像,我想在谷歌地图上给它一些线性和旋转运动。

如何在 GMS 中执行此操作?请帮帮我。

【问题讨论】:

标签: iphone ios xcode google-maps gmsmapview


【解决方案1】:

您可以将图像添加为标记,然后使用该标记的 layer 属性使用CoreAnimation 添加一些动画

查看文档:developers.google.com/maps/documentation/ios/reference/

【讨论】:

【解决方案2】:

实际上我通过使用以下代码和 GMSMarker 方法 setPosition: 解决了这个问题。 以下代码为图像提供旋转并使用 setPosition:我们可以将标记/图像放置在任何地方。 两者的结合提供了所需的线性和旋转运动。

- (UIImage *)imageRotatedByDegrees:(CGFloat)degrees image: (UIImage*) image
{
    CGSize size = image.size;;

    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextTranslateCTM( context, 0.5f * size.width, 0.5f * size.height ) ;
    CGContextRotateCTM (context, DegreesToRadians(degrees));

    [ image drawInRect:(CGRect){ { -size.width * 0.5f, -size.height * 0.5f }, size } ] ;
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

【讨论】:

  • 我试过你的代码,但我的标记图标似乎缩小了。你知道如何解决这个问题吗?
  • @RahulVyas 如果您的图像不是方形的,可能就是这种情况。尝试更改 0.5 乘数。
  • 尝试了另一种有效的方法。你知道我们怎样才能像优步一样平稳地转动汽车吗?我正在更新我的服务器上的标题值。
  • @RahulVyas 您将不得不使用网络套接字来定位汽车。基本上你需要的是服务器和应用程序之间的持久连接。
【解决方案3】:

这是一个简单的过程。首先使用此函数旋转标记以获得正确的标题:

+(float)getBearing:(CLLocationCoordinate2D)locations1 andSecond:(CLLocationCoordinate2D)locattion2{
    float fLat = degreesToRadians(locations1.latitude);
    float fLng = degreesToRadians(locations1.longitude);
    float tLat = degreesToRadians(locattion2.latitude);
    float tLng = degreesToRadians(locattion2.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;
    }
}

然后将标记旋转到新标题:

YOUR_MARKER.rotation = CALCULATED_HEADING - 180;

现在,最后一步是平滑地为您的驱动程序制作动画

[CATransaction begin];
[CATransaction setAnimationDuration:3.1];
[YOUR_MARKER setPosition:NEW_LOCATION_COORDINATES];
[CATransaction commit];

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多