ios - GMSMarker 在 GMSPolyline 上的拐角处弹跳

标签 ios objective-c google-maps-api-3 gps gmsmapview

我正在开发一个导航应用程序并跟踪用户在已绘制的 GMSPolyline 上的当前移动。当用户直行时效果很好。现在,假设 GMSPolyline 上有右转/左转或掉头,现在根据我的 GPS 位置,我在转弯前 20 米左右得到一个更新,在 30 米后得到另一个更新。

我的 GPS 无法收集恰好在转折点存在的点。在这种情况下,我的 GMSMarker 从 x 点跳到 y 点,如果我应用动画,它就会沿对角线移动,而不是沿着 GMSPolyline 弧线或曲线移动。 请建议我如何从 GMSPolyline 收集那些缺失的点或为 GMS 标记显示一些动画,以便用户可以看到他实际上正在打开折线。

我附上一张示例图片。红线可以理解为 GMSPolylines,蓝点是我通过 CLLocation Manager 接收到的 GPS 坐标。

enter image description here

最佳答案

// 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);
    }
}

我们之前做过这个,但是不能贴代码,但是希望你能得到一个设计层面的想法。

希望对您有所帮助。

关于ios - GMSMarker 在 GMSPolyline 上的拐角处弹跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41060922/

相关文章:

ios - 跳转到 CSV 文件中的行

ios - 未调用 WKHTTPCookieStore.setCookie 完成处理程序

ios - 如何在页面 ScrollView 中随机化图像

ios - SWRevealViewController 滑动手势导致应用程序崩溃

javascript - Google map 更改单选按钮选择上 JSON 覆盖的填充颜色

ios - 即使设置了 contentSize,UIScrollView 也不会滚动

ios - 使按钮从数组中读取字符串

java - 用objective-c在java中模拟接口(interface)

javascript - 离开页面后返回谷歌地图 V3 上的标记

javascript - Google map 中 SVG 层的自定义鼠标交互