繁体   English   中英

ClLocationCoordinate2D的NSMutableArray

[英]NSMutableArray of ClLocationCoordinate2D

我正在尝试创建然后检索CLLocationCoordinate2D对象的数组,但由于某种原因,该数组始终为空。

我有:

NSMutableArray *currentlyDisplayedTowers;
CLLocationCoordinate2D new_coordinate = { currentTowerLocation.latitude, currentTowerLocation.longitude };
[currentlyDisplayedTowers addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)] ];

我也试过这个来添加数据:

[currentlyDisplayedTowers addObject:[NSValue value:&new_coordinate withObjCType:@encode(struct CLLocationCoordinate2D)] ];

无论哪种方式,[currentDisplayedTowers count]总是返回零。 什么想法可能会出错?

谢谢!

要留在对象CLLocation ,可以创建CLLocation实例并将其添加到可变数组中。

CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];

为了得到CLLocationCoordinate从结构背CLLocation ,打电话coordinate的对象。

CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];

正如SB所说,确保您的阵列已分配并初始化。

您可能还想在第二个代码段中使用NSValue包装。 然后解码就像:

NSValue *wrappedCoordinates = [currentlyDisplayedTowers lastObject]; // or whatever object you wish to grab
CLLocationCoordinate2D coordinates;
[wrappedCoordinates getValue:&coordinates];

你需要分配你的数组。

NSMutableArray* currentlyDisplayedTowers = [[NSMutableArray alloc] init];

然后你可以使用它。 完成后请务必致电释放或使用其他工厂方法。

currentlyDisplayedTowers = nil导致了所有问题。 此外,以前的init和alloc建议是必要的。 谢谢大家的帮助!

对于有此问题的其他人,如果您计划使用MapKit ,还有另一种解决方案。

(当然,我说IF的原因是因为导入一个像MapKit这样的模块纯粹是为了一个方便的包装器方法可能不是最好的举动..但是在这里你去。)

@import MapKit;

然后只需在需要时使用MapKit的坐标值包装器:

[coordinateArray addObject:[NSValue valueWithMKCoordinate:coordinateToAdd]];

在你的例子..

[currentlyDisplayedTowers addObject:[NSValue valueWithMKCoordinate:new_coordinate]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM