Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用Google Maps SDK在地图上绘制错误的多段线_Ios_Objective C_Google Maps_Google Maps Sdk Ios_Polyline - Fatal编程技术网

Ios 使用Google Maps SDK在地图上绘制错误的多段线

Ios 使用Google Maps SDK在地图上绘制错误的多段线,ios,objective-c,google-maps,google-maps-sdk-ios,polyline,Ios,Objective C,Google Maps,Google Maps Sdk Ios,Polyline,我正在尝试使用谷歌地图SDK在地图上绘制路线。 是我正在调用的URL,我将JSON响应解析为坐标数组: id jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; int points_count = 0; points_count = [[[[[[jsonResponse objec

我正在尝试使用谷歌地图SDK在地图上绘制路线。 是我正在调用的URL,我将JSON响应解析为坐标数组:

    id jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

    int points_count = 0;
    points_count = [[[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];

    NSArray *steps = nil;
    if (points_count && [[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
    {
        steps = [[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
    }

    NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:points_count];
    for (int i = 0; i < points_count; i++)
    {
        NSDictionary *start;
        NSDictionary *finish;

        double st_lat = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lat"] doubleValue];
        double st_lon = [[[[steps objectAtIndex:i] objectForKey:@"start_location"] valueForKey:@"lng"] doubleValue];

        if (st_lat > 0.0f && st_lon > 0.0f)
        {
            start = @{ @"latitude" : [NSNumber numberWithDouble:st_lat], @"longitude" : [NSNumber numberWithDouble:st_lon] };
        }

        double end_lat = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lat"] doubleValue];
        double end_lon = [[[[steps objectAtIndex:i] objectForKey:@"end_location"] valueForKey:@"lng"] doubleValue];

        if (end_lat > 0.0f && end_lon > 0.0f)
        {
            finish = @{ @"latitude" : [NSNumber numberWithDouble:end_lat], @"longitude" : [NSNumber numberWithDouble:end_lon] };
        }

        [coordinates addObject:@{ @"start" : start, @"finish" : finish }];
    }

为什么画成这样,而不是自己上街


我在这里做错了什么?

所以问题是我使用了
开始位置
结束位置
,而不是
多段线
->
。将我的代码修复为:

请求URL例如:
https://maps.googleapis.com/maps/api/directions/json?origin=40.716072,-74.008836&目的地=40.697545,-73.983892&传感器=假&航路点=优化:真&模式=驾驶

id jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

int points_count = 0;
points_count = [[[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];

NSArray *steps = nil;
if (points_count && [[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
{
    steps = [[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
}

NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:points_count];
for (int i = 0; i < points_count; i++)
{
    NSString *toDecode = [[[steps objectAtIndex:i] objectForKey:@"polyline"] valueForKey:@"points"];
    NSArray *locations = [AppUtils decodePolylineWithString:toDecode];

    for (int i = 0 ; i < locations.count ; i++)
    {
        if (i != locations.count - 1) {
            CLLocation *start = [locations objectAtIndex:i];
            CLLocation *finish = [locations objectAtIndex:i + 1];
            [coordinates addObject:@{ @"start" : start, @"finish" : finish }];
        }
    }
}

GMSMutablePath *path = [GMSMutablePath path];
for (NSDictionary *d in directions)
{
    CLLocation *start = d[@"start"];
    CLLocation *finish = d[@"finish"];

    [path addCoordinate:start.coordinate];
    [path addCoordinate:finish.coordinate];
}

GMSPolyline *line = [GMSPolyline polylineWithPath:path];
line.strokeColor = [UIColor redColor];
line.strokeWidth = 2.0f;
line.map = self.mapView;


+ (NSArray*)decodePolylineWithString:(NSString *)encodedString
{
    NSMutableArray *coordinates = [NSMutableArray array];
    const char *bytes = [encodedString UTF8String];
    NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger idx = 0;

    NSUInteger count = length / 4;
    CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
    NSUInteger coordIdx = 0;

    float latitude = 0;
    float longitude = 0;
    while (idx < length) {
       char byte = 0;
        int res = 0;
        char shift = 0;

        do {
            byte = bytes[idx++] - 63;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);

        float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
        latitude += deltaLat;

        shift = 0;
        res = 0;

        do {
            byte = bytes[idx++] - 0x3F;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);

        float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
        longitude += deltaLon;

        float finalLat = latitude * 1E-5;
        float finalLon = longitude * 1E-5;

        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
        coords[coordIdx++] = coord;
        CLLocation *location = [[CLLocation alloc] initWithLatitude:finalLat longitude:finalLon];
        [coordinates addObject:location];

        if (coordIdx == count) {
            NSUInteger newCount = count + 10;
            coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
            count = newCount;
        }
    }

    free(coords);
    return coordinates;
}
id jsonResponse=[NSJSONSerialization JSONObjectWithData:responseobjectoptions:NSJSONReadingMutableContainers错误:nil];
int points_count=0;
points_count=[jsonResponse objectForKey:@“routes”]objectAtIndex:0]objectForKey:@“legs”]objectAtIndex:0]objectForKey:@“steps”]count];
NSArray*步数=零;
如果(点数和[[[jsonResponse objectForKey:@“routes”]objectAtIndex:0]objectForKey:@“legs”]count])
{
步骤=[jsonResponse objectForKey:@“路由”]objectAtIndex:0]objectForKey:@“腿”]objectAtIndex:0]objectForKey:@“步骤”];
}
NSMUTABLEARRY*坐标=[[NSMUTABLEARRY alloc]initWithCapacity:点计数];
对于(int i=0;i>1):(res>>1));
纬度+=三角洲;
移位=0;
res=0;
做{
字节=字节[idx++]-0x3F;
res |=(字节&0x1F)=0x20);
浮点数=((res&1)~(res>>1):(res>>1));
经度+=三角洲;
浮点数=纬度*1E-5;
浮动最终=经度*1E-5;
CLLocationCoordinate2D coord=CLLocationCoordinate2DMake(最终,最终);
coords[coordIdx++]=coord;
CLLocation*location=[[CLLocation alloc]initWithLatitude:finalLat经度:finalLon];
[坐标添加对象:位置];
如果(坐标dx==计数){
NSU整数newCount=计数+10;
coords=realloc(coords,newCount*sizeof(CLLocationCoordinate2D));
计数=新计数;
}
}
免费(coords);
返回坐标;
}
我知道这有点脏,但这是工作,而且效果很好


享受。

试试这个,它画出了精确的路径,就像在驾驶模式-街道模式中一样,以下是我的代码:


@adijazz91如果我保持在同一位置,我会崩溃,你有样品发送吗
id jsonResponse = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

int points_count = 0;
points_count = [[[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"] count];

NSArray *steps = nil;
if (points_count && [[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] count])
{
    steps = [[[[[jsonResponse objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
}

NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:points_count];
for (int i = 0; i < points_count; i++)
{
    NSString *toDecode = [[[steps objectAtIndex:i] objectForKey:@"polyline"] valueForKey:@"points"];
    NSArray *locations = [AppUtils decodePolylineWithString:toDecode];

    for (int i = 0 ; i < locations.count ; i++)
    {
        if (i != locations.count - 1) {
            CLLocation *start = [locations objectAtIndex:i];
            CLLocation *finish = [locations objectAtIndex:i + 1];
            [coordinates addObject:@{ @"start" : start, @"finish" : finish }];
        }
    }
}

GMSMutablePath *path = [GMSMutablePath path];
for (NSDictionary *d in directions)
{
    CLLocation *start = d[@"start"];
    CLLocation *finish = d[@"finish"];

    [path addCoordinate:start.coordinate];
    [path addCoordinate:finish.coordinate];
}

GMSPolyline *line = [GMSPolyline polylineWithPath:path];
line.strokeColor = [UIColor redColor];
line.strokeWidth = 2.0f;
line.map = self.mapView;


+ (NSArray*)decodePolylineWithString:(NSString *)encodedString
{
    NSMutableArray *coordinates = [NSMutableArray array];
    const char *bytes = [encodedString UTF8String];
    NSUInteger length = [encodedString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    NSUInteger idx = 0;

    NSUInteger count = length / 4;
    CLLocationCoordinate2D *coords = calloc(count, sizeof(CLLocationCoordinate2D));
    NSUInteger coordIdx = 0;

    float latitude = 0;
    float longitude = 0;
    while (idx < length) {
       char byte = 0;
        int res = 0;
        char shift = 0;

        do {
            byte = bytes[idx++] - 63;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);

        float deltaLat = ((res & 1) ? ~(res >> 1) : (res >> 1));
        latitude += deltaLat;

        shift = 0;
        res = 0;

        do {
            byte = bytes[idx++] - 0x3F;
            res |= (byte & 0x1F) << shift;
            shift += 5;
        } while (byte >= 0x20);

        float deltaLon = ((res & 1) ? ~(res >> 1) : (res >> 1));
        longitude += deltaLon;

        float finalLat = latitude * 1E-5;
        float finalLon = longitude * 1E-5;

        CLLocationCoordinate2D coord = CLLocationCoordinate2DMake(finalLat, finalLon);
        coords[coordIdx++] = coord;
        CLLocation *location = [[CLLocation alloc] initWithLatitude:finalLat longitude:finalLon];
        [coordinates addObject:location];

        if (coordIdx == count) {
            NSUInteger newCount = count + 10;
            coords = realloc(coords, newCount * sizeof(CLLocationCoordinate2D));
            count = newCount;
        }
    }

    free(coords);
    return coordinates;
}