gpt4 book ai didi

ios - 在 C 数组中存储 CLLocation2DCoordinates - 动态设置数组大小不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:19 25 4
gpt4 key购买 nike

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

}

最佳答案

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

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 .还有 KMLViewer来自 Apple 的示例应用程序,但我认为示例代码仅解析多段线。

关于评论中的问题:

Is there a better mechanism to do that than a plist?

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

关于ios - 在 C 数组中存储 CLLocation2DCoordinates - 动态设置数组大小不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12100015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com