GMSMarker在GMSPolyline的各个角落跳动 [英] GMSMarker bouncing around the corners on GMSPolyline

查看:502
本文介绍了GMSMarker在GMSPolyline的各个角落跳动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作导航应用程序,并跟踪用户在已绘制的GMSPolyline上的当前动作。它在用户直行时效果很好。现在如果假设在GMSPolyline上有右转/左转或掉头,现在按照我的GPS位置,我会在转弯前拿到一个20米左右的更新,而在30米后转向另一个。

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

我附上了一张样本图片。红线可以理解为GMSPolylines,蓝点是我用CLLocation Manager收到的GPS坐标。

p>

解决方案

  //当在GMSMapView上绘制折线时
GMSPath * path = [GMSPath pathFromEncodedPath :strEncodedPolyline]; //解码编码的折线字符串以转换为位置。
//将所有路径点捕获到全局数组中,以便我们可以跟踪用户稍后旅行的方式。
arrLocationsOnPolyline = [NSMutableArray new]; //在为
填充(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];
}
//这里,循环结束后,我们将获取所有路径点作为位置到arrLocationsOnPolyline


//现在位于-locationManager:didUpdateLocations:委托方法
// 1.在arrLocationsOnPolyline中查找用户当前位置的最近路径点的索引,让我们将其称为nFoundAtIndexTemp。
// FMI:循环遍历arrLocationsOnPolyline并找到最近点的用户当前位置的索引,

//保存全局nLastFoundAtIndex变量并将其默认值设置为-1(in -viewDidLoad或者某处),
// 2.如果(nLastFoundAtIndex> = 0&&n; nFoundAtIndexTemp>(nLastFoundAtIndex + 10))//检查
//(意味着应用程序没有收到位置更新,但用户实际上是在多段线上绘制超过10点)
{
// 3. Hurray,你得到了他,现在从当前存储的最近路径点索引和当前最近点路径点索引arrLocationsOnPolyline
}


// 4.用当前状态更新nLastFoundAtIndex
nLastFoundAtIndex = nFoundAtIndexTemp;

//代码通过错过的点为用户位置标记添加动画
//使用用户错过的数组调用此函数(可能来自-locationManager:didUpdateLocations :),标记将通过点。
#pragma mark - 通过错过的坐标进行动画制作
- (void)animateMarker:(GMSMarker *)markerUserLocation throughTheMissedLocations:(NSMutableArray< CLLocation *> *)arrMissedLocations
{
@try
{
CLLocation * locTemp = arrMissedLocations.firstObject;
if(locTemp)
{
[CATransaction begin];
NSTimeInterval nAnimationDuration = 0.1; //根据你的需要更新这个值。
[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(@%s function%@处的异常,__ PRETTY_FUNCTION __,exception.debugDescription);
}
}];
}
@catch(NSException * exception)
{
NSLog(@%s function%@处的异常,__ PRETTY_FUNCTION __,exception.debugDescription);
}
}
[CATransaction commit];
{
}
@catch(NSException * exception)
{
NSLog(@%s function%@处的异常,__ PRETTY_FUNCTION __,exception.debugDescription);




$ b $ p
$ b

我们之前做过,但是不能发布代码,但希望你会得到一个设计水平的想法。



希望有帮助。


I am working on a navigation app and tracking user's current movement on an already drawn 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.

My GPS fails to collect the points that will exist just at the turning point. 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. 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.

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.

解决方案

// 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.

这篇关于GMSMarker在GMSPolyline的各个角落跳动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆