[英]GMSMarker bouncing around the corners on GMSPolyline
I am working on a navigation app and tracking user's current movement on an already drawn GMSPolyline. 我正在开发导航应用程序并跟踪用户在已绘制的GMSPolyline上的当前移动。 It works good when the user is going straight. 当用户直接行进时它很好用。 Now if, suppose there is right / left turn or a u-turn on the GMSPolyline, now as per my GPS location I get one update around 20 meters before taking a turn and another after 30 meters. 现在如果,假设GMSPolyline上有左/右转弯或掉头,现在根据我的GPS位置,我在转弯前大约20米处获得一次更新,在30米之后获得另一次更新。
My GPS fails to collect the points that will exist just at the turning point. 我的GPS无法收集将在转折点存在的点。 In this case my GMSMarker jumps from point x to y and if I apply animation then it moves diagonally and does not move along the GMSPolyline arcs or curves. 在这种情况下,我的GMSMarker从点x跳到y,如果我应用动画,那么它会对角移动,不会沿着GMSPolyline弧或曲线移动。 Please suggest me how can I collect those missing points from GMSPolyline or show some animation for GMS marker so that user can see he is actually turning on the polyline. 请建议我如何从GMSPolyline收集那些缺失点或为GMS标记显示一些动画,以便用户可以看到他实际上正在打开折线。
I am attaching one sample image. 我附上了一张示例图片。 Red lines can be understood as GMSPolylines and blue dots are the GPS coordinates that I receive with CLLocation Manager. 红线可以理解为GMSPolylines,蓝点是我通过CLLocation Manager收到的GPS坐标。
// 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);
}
}
We did this earlier,But can't post code, But hopefully you'll get a design level idea. 我们之前做过这个,但不能发布代码,但希望你会得到一个设计级别的想法。
Hope that helps. 希望有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.