2

我正在尝试编写一个使用地图视图添加叠加层的应用程序。覆盖数据从文件加载到 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;

}
4

1 回答 1

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 格式并让内置的 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)。如有必要,这可能值得一个单独的问题。

于 2012-08-27T22:06:27.650 回答