2016-12-09 6 views
3

私はナビゲーションアプリを開発し、既に描画されたGMSPolylineでユーザーの現在の動きを追跡しています。ユーザーがまっすぐ進むとうまくいきます。今、GMSPolylineに右折または左折またはUターンがあるとしたら、私のGPS位置ごとに、ターン前に20メートル、30メートル後にもう一つ更新します。GMSMarkerがGMSPolylineのコーナーを跳ね返る

私のGPSは、転換点にあるポイントを収集できません。この場合、私のGMSMarkerはポイントxからyにジャンプします。アニメーションを適用すると、斜めに移動し、GMSPolylineの円弧または曲線に沿って移動しません。 GMSPolylineからこれらの欠けているポイントを収集する方法や、GMSマーカーのアニメーションを表示して、ユーザーが実際にポリラインをオンにしていることを確認する方法を教えてください。

1枚のサンプル画像を添付しています。赤い線はGMSPolylines、青い点はCLLocation Managerで受信したGPS座標です。

enter image description here

+0

回答がうまくいく場合は、回答としてマークしてください。 – Efren

答えて

2
// While drawing polyline on GMSMapView 
GMSPath *path = [GMSPath pathFromEncodedPath:strEncodedPolyline]; // Decoding encoded polyline string for converting to locations. 
// Capture all path points in to a global array,So that we can track how user is travelling later. 
arrLocationsOnPolyline = [NSMutableArray new]; // Make it fresh before filling 
for (int counter = 0; counter < path.count; ++counter) 
{ 
    CLLocationCoordinate2D coordinate = [path coordinateAtIndex:counter]; 
    CLLocation *locTemp = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude]; 
    [arrLocationsOnPolyline addObject:locTemp]; 
} 
// Here,After loop ending, we'll get all path points as locations in to arrLocationsOnPolyline 


// Now in -locationManager:didUpdateLocations: delegate method, 
// 1. Find the index of nearest path point to user's current location in arrLocationsOnPolyline,Lets call it as nFoundAtIndexTemp. 
// FMI : loop through the arrLocationsOnPolyline and find the nearest point's index to user's current location, 

// Hold a global nLastFoundAtIndex variable and make it's default value as -1(in -viewDidLoad or somewhere), 
// 2. Check 
if (nLastFoundAtIndex >= 0 && nFoundAtIndexTemp > (nLastFoundAtIndex + 10)) // (Means app didn't received location updates but user actually traveled through more than 10 points on poyline drawn) 
{ 
    // 3. Hurray,You got him,Now animate your blue current location marker from the location at last stored nearest path point index and current nearest path point index of arrLocationsOnPolyline 
} 


// 4. Update nLastFoundAtIndex with current state 
nLastFoundAtIndex = nFoundAtIndexTemp; 

// Code To Animate user location marker through the missed points 
// Call this function with array of user missed points(Probably from -locationManager:didUpdateLocations:),Marker will be animated through the points. 
#pragma mark - Animating through the missed coordinates 
-(void)animateMarker:(GMSMarker *)markerUserLocation throughTheMissedLocations:(NSMutableArray <CLLocation *> *)arrMissedLocations 
{ 
    @try 
    { 
     CLLocation *locTemp = arrMissedLocations.firstObject; 
     if(locTemp) 
     { 
      [CATransaction begin]; 
      NSTimeInterval nAnimationDuration = 0.1; // Update this value as per your needs. 
      [CATransaction setAnimationDuration:nAnimationDuration]; 
      markerUserLocation.position = locTemp.coordinate; 
      if(arrMissedLocations.count >= 1) 
      { 
       @try 
       { 
        [CATransaction setCompletionBlock:^ 
        { 
         @try 
         { 
          [arrMissedLocations removeObject:locTemp]; 
          [self animateMarker:markerUserLocation throughTheMissedLocations:arrMissedLocations]; 
         } 
         @catch (NSException *exception) 
         { 
          NSLog(@"exception at %s function %@",__PRETTY_FUNCTION__,exception.debugDescription); 
         } 
        }]; 
       } 
       @catch (NSException *exception) 
       { 
        NSLog(@"exception at %s function %@",__PRETTY_FUNCTION__,exception.debugDescription); 
       } 
      } 
      [CATransaction commit]; 
     } 
    } 
    @catch (NSException *exception) 
    { 
     NSLog(@"exception at %s function %@",__PRETTY_FUNCTION__,exception.debugDescription); 
    } 
} 

私たちは、この以前をした、しかし、コードを投稿することはできません、しかし、うまくいけば、あなたは設計レベルのアイデアを得るでしょう。

希望に役立ちます。

+0

あなたの助けてくれてありがとう、私はこれを試してみます。 – NewStackUser

+0

いったん私がユーザが見逃した座標を得たら、どのアニメーションをアニメーション用のマーカで使うべきですか?私はいろいろなアニメーションを使っていましたが、順番に順調に動かず、各CLLocationポイントを訪れません。 – NewStackUser

+0

私の更新された答えを確認してください、更新は、不足しているポイント配列を通してマーカーをアニメートする機能を持っています。 –

関連する問題