如果用户在iPhone中关注折线,则逐渐减少折线

时间:2014-02-28 07:16:20

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

我正在创建一个导航iPhone应用,可在用户想要的polylineuser location之间创建destination

我可以创建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;
}

1 个答案:

答案 0 :(得分:1)

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