【问题标题】:Storing CLLocation2DCoordinates in C array - dynamically setting array size doesn't work在 C 数组中存储 CLLocation2DCoordinates - 动态设置数组大小不起作用
【发布时间】:2012-08-23 21:11:24
【问题描述】:

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

}

【问题讨论】:

  • 我也尝试像这样设置坐标的数组大小: CLLocation2DCoordinate coord[noOfPoints];但是,这也不起作用。
  • 请添加-loadDatafromFile的代码
  • 你的txt文件真的是如图所示的格式吗? NSArray 的 initWithContentsOfFile 方法要求文件为 plist (xml) 格式。如果要按原样读取 txt 文件,则必须使用 NSString 的 stringWithContentsOfFile 方法,然后手动解析不可靠且不必要的工作的字符串。如果你把你的数据以 plist 格式,它会更容易读入一个字典数组,然后可以很容易地用来构造 MKPolygon 对象。
  • 感谢您的提示。但是数据已经是 plist 格式了。我上面发布的是加载 plist 后 NSArray 的内容。但是,您能否提供更多关于如何使用字典构造 MKPolygons 的信息?
  • PS:这是我用来加载数据的 plist 开头的样子:ttp://www.apple.com/DTDs/PropertyList-1.0.dtd" rel="nofollow" target="_blank">apple.com/DTDs/PropertyList-1.0.dtd"> Revier1_Poly20447.22012717 8.80838834747.23945854005158.79568994231076

标签: ios nsarray mkoverlay


【解决方案1】:

首先,这是您现有代码的问题,即手动将单个元素解析为一个大型平面数组...

for j 循环存在以下问题:

  • j&lt;noOfPoints+1 应该是 j &lt; (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 示例应用程序,尽管我认为示例代码仅解析折线。

关于您在 cmets 中的问题:

有没有比 plist 更好的机制来做到这一点?

这取决于您希望存储多少数据。如果 plist 对您的应用程序来说不够快,则必须切换到允许随机访问数据的方法(例如 SQL 数据库或多个 plist)。如有必要,这可能值得一个单独的问题。

【讨论】:

  • 感谢您提供如此详细的答案。我会马上解决的。关于我要处理的数据/多边形的数量:我估计大约有 500 个多边形。所以我希望plist的方式应该是可行的。
猜你喜欢
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
相关资源
最近更新 更多