マップビューを使用してオーバーレイを追加するアプリを作成しようとしています。オーバーレイデータは、ファイルから次のような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;
}