11

我有我的要求就像當前位置一個視圖將顯示。它會旋轉,如果設備是旋轉或位置將改變。我研究很多,但得到的所有代碼有固定的位置或角度在某個位置,但我沒有修復位置。任何人都可以在正確的方向驅動我。旋轉GMSMarker的方向用戶面對

我也用輪轉屬性的GMSMarker,但它不工作。

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

更新:我有旋轉的方法,但有什麼辦法來旋轉GMSMarker因爲對於變換沒有方法。

他們在谷歌地圖上旋轉他們的車怎麼樣?

enter image description here

+0

旋轉圖像'度'應該是您爲之前的標題度儲存的一些變量。 – zcui93

+0

@ zcui93,但在第一次我怎麼找到那個程度 –

+0

你只是給它分配一個默認值,對應你裝載時的默認箭頭方向。 – zcui93

回答

3

當前位置 'CLLocation' 對象有一個名爲 '課程'

@property(readonly, nonatomic) CLLocationDirection course; 
類型CLLocationDirection的

(的typedef雙)屬性其是位置的角度。

汽車旋轉時,您需要在後端,方向以及經度和緯度的額外區域。使用這些信息通過應用轉換上的UIView

CGAffineTransformMakeRotation(M_PI * (course_of_location)/180.0); 
+0

我已經試過這個,但仍然沒有給我答案,我把這個「CGAffineTransformMakeRotation(M_PI *(course_of_location)/ 180.0);」碼。因爲標記沒有任何變換屬性。 –

+0

@Sunil。 CGAffineTransform(rotationAngle:(M_PI *(course_of_location)/ 180)如何設置這個CGAffineTransformMakeRotation在swift 3 –

+0

CGAffineTransform0)) –

4

你可以做一些像轉動汽車 -

-(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; 
} 
+0

我加了這段代碼。這是工作,但我面臨的一個問題是標記是在該位置和更新的位置上跳轉。而且即使我不從我的位置移動,標記也會不斷旋轉。我該怎麼辦? –

+0

@Haider。我如何設置基於經度和緯度的標記位置。這些來自服務器。我如何設置旋轉.. –

0
marker.rotation = course_of_location; 

注:旋轉只腳運動過程中會發生。在靜態設備的情況下,會有-1的location.course值。這也與設備方向無關。如果您正在尋找根據設備的標題,然後做移動信號引腳

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { 
marker.rotation = (manager.heading.trueHeading) * M_PI/180.0f; } 
+0

請解釋答案,爲什麼它會起作用? – rptwsthi

+2

旋轉是GMSMarker的屬性,CLLocation類提供了哪個實際的移動方向(不是設備的方向)。所以如果你設置了上面的代碼,即使你可以簡單地做'marker.rotation = location.course;當你移動時它會工作。當沒有移動時,當然值是-1,它不會顯示任何旋轉。 – Spydy

1

//只是這樣做只是

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{ 
    double heading = newHeading.trueHeading; 
    marker.groundAnchor = CGPointMake(0.5, 0.5); 
    marker.rotation = heading; 
    marker.map = mapView; 
} 
2

我們可以根據課程性質CLLocation類

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 
+0

它會工作正常 –