我正在尝试编写一个使用地图视图添加叠加层的应用程序。覆盖数据从文件加载到 NSArray 中,如下所示: ...
Revier355_2_Poly, //name of the polygon (string)
6, //no of coordinates of polygon (int)
47.4038673907557, //latitude of first coordinate (double)
8.5247885145479, //longitude of first coordinate (double)
47.4035082236459,
8.52436382148047,
47.4031174379495,
8.52493568571008,
47.403348653295,
8.52586444637587,
47.4037666977377,
8.52550468311683,
47.4038673907557,
8.5247885145479
...下一个带有名称、点数、坐标的多边形。
现在我想将上述数据转换为 MKPolygons 并将它们放在 NSDictionary 中。为此,我想创建包含文件中每个多边形坐标的 C 数组。这需要在运行时动态设置数组中的项目数。这是我的问题:我无法做到这一点。你能帮忙或指出正确的方向吗?
这是我写的代码
int i = 0;
NSArray *data = [self loadDatafromFile];
NSMutableDictionary *overlays = [NSMutableDictionary new];
for ( i=0; i<data.count;i++)
{
if ([[data objectAtIndex: i] isKindOfClass: [NSString class]])
//start of new Polygon called Revier
{
//Name of Revier
NSString *polyName = (NSString*)[data objectAtIndex: i];
//PointCount is second entry after name
int noOfPoints = [[data objectAtIndex: (i+1)] intValue];
//Revier coordinates Error while setting Array size dynamically
CLLocationCoordinate2D *coord = malloc(sizeof(CLLocationCoordinate2D) * noOfPoints);
for (int j=0; j<noOfPoints+1; j+=2)
{
CLLocationCoordinate2D currCoord = CLLocationCoordinate2DMake([[data objectAtIndex:j+i] doubleValue],[[data objectAtIndex:j+i+1] doubleValue]);
coord[j] = currCoord;
}
MKPolygon *p = [MKPolygon polygonWithCoordinates:coord count:noOfPoints];
[overlays setObject: p forKey:polyName];
free(coord);
}
}
这是加载数据的代码...
- (NSArray *)loadDatafromFile
{
NSArray* myArr = [[NSArray alloc] initWithContentsOfFile:[self filePath]];
NSLog(@"%@",[self filePath]);
return myArr;
}
- (NSString *) filePath
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"polygone" ofType:@"txt"];
NSLog(@"%@",path);
return path;
}