2016-12-09 25 views
3

我工作的一个导航应用和跟踪上已经绘制GMSPolyline用户当前的运动。它在用户直行时效果很好。现在,如果,假设有右/左转向或在GMSPolyline调头,现在按我的GPS位置我得到约20仪表之一更新以一转,另一个后30米之前。GMSMarker蹦跳着角落上GMSPolyline

我的GPS无法收集只会在转折点上存在的点。在这种情况下我GMSMarker从点x跳到y和,如果我申请的动画则对角线移动,沿GMSPolyline弧线或曲线不动。 请建议我我如何能收集GMSPolyline的失分还是显示了一些动画大湄公河次区域的标记,使用户可以看到他对折线实际转弯。

我附上一个样本图像。红线可以理解为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

请检查我的更新答案,更新有一个功能,通过错过点数组为动画标记设置动画。 –