【发布时间】:2014-03-07 09:37:16
【问题描述】:
通过下面的代码,我可以获得如下输出:
0
1
1
这一行正在生成来自外部数据库的布尔值(您可以在下面的完整代码中查看它):BOOL test3 = [test containsCoordinate:tg];
问题:
我想计算前面所说的 BOOL 行生成的 TRUE 布尔值的数量;例如输出,执行该行时找到了多少1。
代码:
-(void)markers{ NSURL *url = [NSURL URLWithString:@"http://example.com/api/s.php"]; data = [NSData dataWithContentsOfURL:url]; NSError *error; NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error]; if (error != nil) { NSLog(@"Could not fetch data !"); } NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", coordi, f_query]]; NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL]; NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; id array2 = [dataDictionary objectForKey:@"routes"]; if([array2 count] > 0) { NSDictionary *routes = [dataDictionary objectForKey:@"routes"][0]; NSDictionary *route = [routes objectForKey:@"overview_polyline"]; NSDictionary *legs = [routes objectForKey:@"legs"][0]; NSString *overview_route = [route objectForKey:@"points"]; CLLocationCoordinate2D location; for (NSDictionary *dictionary in array) { location.latitude = [dictionary[@"latitude"] floatValue]; location.longitude = [dictionary[@"longitude"] floatValue]; CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude); GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path]; BOOL test3 = [test containsCoordinate:tg]; { if (test3 == 1) { polyline.strokeColor = [UIColor colorWithRed:0.259 green:0.698 blue:0.894 alpha:1.0]; marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); }else if (test3 == 0) { polyline.strokeColor = [UIColor colorWithRed:0.647 green:0.839 blue:0.016 alpha:0.7]; marker.position = CLLocationCoordinate2DMake(0, 0); } } } }
我花了一整天的时间寻找如何做,但没有机会:s 关于如何解决它的任何想法? 附言解释这个想法的代码 sn-p 将非常有帮助。
【问题讨论】:
-
为什么你没有一个'int count'并在每次test3为YES时加一
-
不,每次在所需边界内找到坐标时,它都会输出
1,增加一不会得到所需的结果。 -
正如@Emmanuel 所说,您之前已经问过这个问题,并且有不少回复可以帮助您解决问题。重新发布本质上相同的问题似乎有点徒劳。
-
“完全不是想要的答案”?我花时间向您解释它,在您要求我之后用代码对其进行了两次编辑,并且您在这里接受了与我的略有不同的答案。谢谢你告诉我。
标签: ios objective-c