【问题标题】:Gradually decrease polylines if user is following it in iPhone如果用户在 iPhone 中关注折线,则逐渐减少折线
【发布时间】:2014-02-28 07:16:19
【问题描述】:

我正在创建一个导航 iPhone 应用程序,该应用程序在用户想要去的 user locationdestination 之间创建一个 polyline

我可以创建polyline。但我需要做的是,当用户在折线上继续前进时,折线需要逐渐减少,以表明用户正在走上正轨。而且我还需要检测用户是否偏离了路径。

例如,如果用户必须向左走但他没有,我将如何检测到?

任何帮助将不胜感激。谢谢。

编辑:

我已经使用下面的代码来绘制折线

-(void)drawPolyline{
    //Draw polyline via the selected route

    NSMutableArray *polys = [NSMutableArray array];
    GMSMutablePath *path = [GMSMutablePath path];

    NSDictionary *route=[routes objectAtIndex:0];

    //self.strPolyPoints is a string
    self.strPolyPoints=[[route objectForKey:@"overview_polyline"] objectForKey:@"points"];
    NSArray *arrPoints=[self decodePolyLine];
    for(CLLocation *location in arrPoints){
        CLLocationCoordinate2D coordinate=location.coordinate;
        [path addCoordinate:coordinate];
    }

    _lengths = @[@([path lengthOfKind:kGMSLengthGeodesic] / 40)];
    GMSPolyline *polyline = [[GMSPolyline alloc] init];
    polyline.path = path;
    polyline.strokeColor = [UIColor blueColor];
    polyline.geodesic = NO;
    polyline.strokeWidth = 5;
    polyline.map = mapView_;
    [polys addObject:polyline];
    _polys = polys;
    [self tick];
}



- (void)tick {
    //Create steps for polyline(dotted polylines)
    for (GMSPolyline *poly in _polys) {
        poly.spans =
        GMSStyleSpans(poly.path, _styles, _lengths, kGMSLengthGeodesic, _pos);
    }
    _pos -= _step;

    //Animate the polyline like moving from source to destination
    if (kAnimate) {
        __weak id weakSelf = self;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC / 5),
                       dispatch_get_main_queue(),
                       ^{ [weakSelf tick]; });
    }
}

-(NSMutableArray *)decodePolyLine {

    NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[self.strPolyPoints length]];
    [encoded appendString:self.strPolyPoints];

    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
                                options:NSLiteralSearch
                                  range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init];

    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
        printf("[%f,", [latitude doubleValue]);
        printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:loc];

    }
    return array;
}

【问题讨论】:

    标签: ios iphone google-maps google-maps-sdk-ios google-polyline


    【解决方案1】:

    我认为您必须绘制从用户位置到目的地的多段线,在设备位置更改时删除现有的多段线并使用用户的当前位置绘制一条新的多段线。您可以分享一些您已经尝试过的代码,以便我可以更好地帮助您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 2012-08-07
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      相关资源
      最近更新 更多