我不确定我的代码有什么问题。以下是我可能存在问题的3个功能。它是适用于iOS的Google地图应用。
错误消息:由于未捕获的异常终止应用&#39; NSInvalidArgumentException&#39;,原因:&#39; - [__ NSArrayI floatValue]:无法识别的选择器发送到实例0x60000042e680&#39; < / p>
- (void)drawPolygon{
GMSMutablePath *rect = [GMSMutablePath path];
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
event.latitude = [[tappedCoordinates objectAtIndex:i] floatValue];
event.longitude = [[tappedCoordinates objectAtIndex:i] floatValue];
[rect addCoordinate:event];
}
// first tapped point to connect with last point in order to close the polygon.
event.latitude = [[tappedCoordinates objectAtIndex:0] floatValue];
event.longitude = [[tappedCoordinates objectAtIndex:0] floatValue];
[rect addCoordinate:event];
...
}
- (void)addMarker{
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
position.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
position.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
...
}
答案 0 :(得分:1)
你只是没有深入到数组中以获得实际的长/纬度值:
for (int i = 0; i <= [tappedCoordinates count]-1; i++) {
event.latitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:0] floatValue];
event.longitude = [[[tappedCoordinates objectAtIndex:i] objectAtIndex:1] floatValue];
[rect addCoordinate:event];
}
event.latitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:0] floatValue];
event.longitude = [[[tappedCoordinates objectAtIndex:0] objectAtIndex:1] floatValue];
修改强>
只是为了澄清 - [tappedCoordinates objectAtIndex:0]是一个NSArray。当你在它上面调用floatValue
时,NSArray不知道该怎么做,因为它不是一个数值。此外,您的方法的替代方法是拥有一个CGPoint对象数组(实现比嵌套数组稍好):
NSMutableArray *coords = [NSMutableArray new];
CGPoint coord = CGPointMake(1.5f, 2.5f);
[coords addObject:[NSValue valueWithCGPoint: coord]];
CGPoint storedCoord = [[coords objectAtIndex:0] CGPointValue];
NSLog (@"Lat: %f, long: %f", storedCoord.x, storedCoord.y);
您甚至可以更进一步创建一个子类CGPoint(名为Coordinate),它具有纬度和经度属性,可以分别返回x和y(主要是为了提高可读性)。