【发布时间】:2014-07-02 18:06:35
【问题描述】:
我正在尝试使用 Google Maps SDK 在我的地图上绘制路线。 This 是我正在调用的 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 }];
}
然后用这种方法在地图视图上绘图:
GMSMutablePath *path = [GMSMutablePath path];
for (NSDictionary *d in directions)
{
NSDictionary *start = d[@"start"];
NSDictionary *finish = d[@"finish"];
CLLocationCoordinate2D c_start = CLLocationCoordinate2DMake([start[@"latitude"] doubleValue], [start[@"longitude"] doubleValue]);
CLLocationCoordinate2D c_finish = CLLocationCoordinate2DMake([finish[@"latitude"] doubleValue], [finish[@"longitude"] doubleValue]);
[path addCoordinate:c_start];
[path addCoordinate:c_finish];
}
GMSPolyline *line = [GMSPolyline polylineWithPath:path];
line.strokeColor = [UIColor redColor];
line.strokeWidth = 2.0f;
line.map = self.mapView;
为什么它是那样画而不是自己上街?
我在这里做错了什么?
【问题讨论】:
标签: ios objective-c google-maps google-maps-sdk-ios polyline