我正在尝试编写一个使用地图视图添加叠加层的应用。覆盖数据从文件加载到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;
}
答案 0 :(得分:2)
首先,以下是现有代码的问题,即手动将各个元素解析为一个大型平面数组......
for j
循环存在以下问题:
j<noOfPoints+1
应为j < (noOfPoints*2)
,因为要为每个点处理两个数组元素从data
数组获取纬度和经度的索引是错误的(在设置currCoord
的行中)。顺便说一句,如果它被分成多个语句,那么该行将更容易阅读,理解和(特别重要)调试 。更正后的代码可能如下所示:
int latitudeIndex = j+i +2; //need to add additional offset of +2
int longitudeIndex = j+i+1 +2; //need to add additional offset of +2
CLLocationDegrees lat = [[data objectAtIndex:latitudeIndex] doubleValue];
CLLocationDegrees lon = [[data objectAtIndex:longitudeIndex] doubleValue];
CLLocationCoordinate2D currCoord = CLLocationCoordinate2DMake(lat,lon);
用于设置C数组中元素的索引是错误的。在coord[j] = currCoord;
行中,j
最初为0
,但随后会增加2
。因此,您最终设置coord[0]
,coord[2]
等,并将奇数索引保持未初始化状态。相反,添加一个单独的计数器作为设置C数组的索引(尽管它可以从j
计算,一个单独的计数器更清晰,更容易理解或更改):
int coordIndex = 0; //declare and initialize before the for-j loop
for (int j=0;...
{
... //code to set currCoord
coord[coordIndex] = currCoord;
coordIndex++;
}
这些更改应该修复多边形对象的解析和创建,但还有另外两个常见问题:
这两种方法都可以通过使用更好的plist格式来解决,并让内置的plist读取方法为您付出艰苦的努力。您所要做的就是创建C数组。
以下是您可能的替代plist格式:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>PolygonName</key>
<string>Revier355_2_Poly</string>
<key>Coordinates</key>
<array>
<dict><key>Latitude</key><real>47.4038673907557</real><key>Longitude</key><real>8.5247885145479</real></dict>
<dict><key>Latitude</key><real>47.4035082236459</real><key>Longitude</key><real>8.52436382148047</real></dict>
<dict><key>Latitude</key><real>47.4031174379495</real><key>Longitude</key><real>8.52493568571008</real></dict>
<dict><key>Latitude</key><real>47.403348653295</real><key>Longitude</key><real>8.52586444637587</real></dict>
<dict><key>Latitude</key><real>47.4037666977377</real><key>Longitude</key><real>8.52550468311683</real></dict>
<dict><key>Latitude</key><real>47.4038673907557</real><key>Longitude</key><real>8.5247885145479</real></dict>
</array>
</dict>
<dict>
<key>PolygonName</key>
<string>Revier1_Poly</string>
<key>Coordinates</key>
<array>
<dict><key>Latitude</key><real>47.22012717</real><key>Longitude</key><real>8.808388346999999</real></dict>
<dict><key>Latitude</key><real>47.2394585400515</real><key>Longitude</key><real>8.79568994231076</real></dict>
<dict><key>Latitude</key><real>47.1394585400515</real><key>Longitude</key><real>4.89568994231076</real></dict>
</array>
</dict>
</array>
</plist>
读取此plist的代码将是这样的:
NSArray *polygonsArray = [NSArray arrayWithContentsOfFile:path];
NSMutableDictionary *overlays = [NSMutableDictionary new];
//loop through the polygon dictionaries...
for (NSDictionary *polygonDict in polygonsArray)
{
NSString *polyName = [polygonDict objectForKey:@"PolygonName"];
NSArray *coordDictionaries = [polygonDict objectForKey:@"Coordinates"];
//loop through the array of coordinate dictionaries for the
//current polygon to create C array of coordinates...
int noOfPoints = coordDictionaries.count;
CLLocationCoordinate2D *coord = malloc(sizeof(CLLocationCoordinate2D) * noOfPoints);
int coordIndex = 0;
for (NSDictionary *coordDict in coordDictionaries)
{
CLLocationDegrees lat = [[coordDict objectForKey:@"Latitude"] doubleValue];
CLLocationDegrees lon = [[coordDict objectForKey:@"Longitude"] doubleValue];
CLLocationCoordinate2D currCoord = CLLocationCoordinate2DMake(lat,lon);
coord[coordIndex] = currCoord;
coordIndex++;
}
MKPolygon *p = [MKPolygon polygonWithCoordinates:coord count:noOfPoints];
[overlays setObject:p forKey:polyName];
[self.mapView addOverlay:p];
free(coord);
}
请注意,您必须处理的唯一索引计算是C数组。
如果您需要更高级或标准化的格式,您还可以查看KML。还有来自Apple的KMLViewer示例应用程序,但我认为示例代码仅解析折线。
关于评论中的问题:
有没有比plist更好的机制呢?
这取决于您希望存储的数据量。如果plist对于您的应用程序来说不够快,则必须切换到允许随机访问数据的方法(例如,SQL数据库或多个plist)。如有必要,这可能值得一个单独的问题。