【问题标题】:Move and Rotate GMSMarker smoothly along last updated GPS coordinates Swift iOS沿着上次更新的 GPS 坐标平滑移动和旋转 GMSMarker Swift iOS
【发布时间】:2016-05-25 22:06:00
【问题描述】:

我正在使用 GMSMapView。因此,我添加了自定义 GMSMarker 并设置图像(例如自行车图像)并在用户开始移动并更改 locationManager 中标记的角度时为该标记设置动画。

目前我只是在更新 GMSMarker 的位置属性。 但是它给出了 GMSMarker 跳跃效果。 而不是这个我想移动和旋转 GMSMarker 平滑/正确进入 GMSMapView。

如何在 Swift 中实现这一点?谢谢。

   (void)updateLocationoordinates(CLLocationCoordinate2D) coordinates
    { 
     if (marker == nil) {
      marker = [GMSMarker markerWithPosition:coordinates];
      marker.icon = [UIImage imageNamed:IMAGE];
      marker.map = mapView;
    } else{
     marker.position = coordinates; 
          }
    }  

【问题讨论】:

    标签: ios swift cllocationmanager catransaction


    【解决方案1】:

    我也遇到过同样的问题并尝试了很多方法。最后我想出了一个解决方案——在以前的位置点和现在的位置点之间取角度,我使用了 UIImageView 而不是 GMSMarker。

    //in location update method
    CGPoint anglepoint = [myMapView.projection pointForCoordinate:currLocation.coordinate];
    CGFloat angle = [self getAngle:anglepoint];
    if(!isnan(angle)){
         [UIView beginAnimations:nil context:nil];
         [UIView setAnimationDuration:0.8f];
         sampleImgView.transform = CGAffineTransformMakeRotation(angle);
         [sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
         [UIView commitAnimations];
         prevCurrLocation = currLocation;
    }
    
    
    -(CGFloat) getAngle: (CGPoint) touchedPoints
    {
        CGPoint previousLocationPoint = [myMapView.projection pointForCoordinate:prevCurrLocation.coordinate];
    
        CGFloat x1 = previousLocationPoint.x;
        CGFloat y1 = previousLocationPoint.y;
    
        CGFloat x2 = touchedPoints.x;
        CGFloat y2 = touchedPoints.y;
    
        CGFloat x3 = x1;
        CGFloat y3 = y2;
    
        CGFloat oppSide = sqrtf(((x2-x3)*(x2-x3)) + ((y2-y3)*(y2-y3)));
        CGFloat adjSide = sqrtf(((x1-x3)*(x1-x3)) + ((y1-y3)*(y1-y3)));
    
        CGFloat angle = atanf(oppSide/adjSide);
        // Quadrant Identifiaction
        if(x2 < previousLocationPoint.x)
        {
            angle = 0-angle;
        }
    
    
        if(y2 > previousLocationPoint.y)
        {
            angle = M_PI/2 + (M_PI/2 -angle);
        }
        return angle;
    }
    

    将此用于动画:

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5f];
    sampleImgView.transform = CGAffineTransformMakeRotation(angle);
    [sampleImgView setCenter:[myMapView.projection pointForCoordinate:CLLocationCoordinate2DMake(currLocation.coordinate.latitude, currLocation.coordinate.longitude)]];
    [UIView commitAnimations];
    

    这是示例 Imageview:

    sampleImgView = [[UIImageView alloc]init];
    sampleImgView.center = myMapView.center;
    sampleImgView.frame = CGRectMake(0,0, 25, 50);
    sampleImgView.backgroundColor = [UIColor clearColor];
    [myMapView addSubview:sampleImgView];
    

    希望这会有所帮助。

    【讨论】:

    • 如果我想旋转图像的方向那怎么做?谢谢你的回答。
    • 嗨萨蒂什,谢谢!能否请您分享源代码。所以对理解和实施会有帮助!
    • 我再次更新了我的答案。如果您需要具体信息,请告诉我。
    • 请帮忙!正如我之前问过的!提前致谢!
    • 您清楚创建 GMSMapView 和 GMS 标记吗?
    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    相关资源
    最近更新 更多