2016-02-15 96 views
2

我正在使用GMSMapView。因此,我添加了自定義GMSMarker,並在用戶開始移動並更改locationManager中標記的角度時設置圖像(例如自行車圖像)併爲該標記設置動畫。沿着最後更新的GPS座標平滑移動並旋轉GMSMarker Swift iOS

目前我只是更新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; 
      } 
    } 

回答

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

希望這有助於。

+0

如果我想旋轉圖像的方向,那麼怎麼樣?謝謝你的回答。 – Captain

+0

Hi Riju,請檢查更新的答案。 –

+0

嗨Satish,謝謝!你可以分享一下源代碼嗎?因此,理解和實施會很有幫助! – Captain