简体   繁体   English

ClLocationCoordinate2D的NSMutableArray

[英]NSMutableArray of ClLocationCoordinate2D

I'm trying to create then retrieve an array of CLLocationCoordinate2D objects, but for some reason the array is always empty. 我正在尝试创建然后检索CLLocationCoordinate2D对象的数组,但由于某种原因,该数组始终为空。

I have: 我有:

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

I've also tried this for adding the data: 我也试过这个来添加数据:

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

And either way, the [currentlyDisplayedTowers count] always returns zero. 无论哪种方式,[currentDisplayedTowers count]总是返回零。 Any ideas what might be going wrong? 什么想法可能会出错?

Thanks! 谢谢!

To stay in object land, you could create instances of CLLocation and add those to the mutable array. 要留在对象CLLocation ,可以创建CLLocation实例并将其添加到可变数组中。

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

To get the CLLocationCoordinate struct back from CLLocation , call coordinate on the object. 为了得到CLLocationCoordinate从结构背CLLocation ,打电话coordinate的对象。

CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];

As SB said, make sure your array is allocated and initialized. 正如SB所说,确保您的阵列已分配并初始化。

You'll also probably want to use NSValue wrapping as in your second code snippet. 您可能还想在第二个代码段中使用NSValue包装。 Then decoding is as simple as: 然后解码就像:

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

You need to allocate your array. 你需要分配你的数组。

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

Then you can use it. 然后你可以使用它。 Be sure to call release when you are done with it or use another factory method. 完成后请务必致电释放或使用其他工厂方法。

I had currentlyDisplayedTowers = nil which was causing all the problems. currentlyDisplayedTowers = nil导致了所有问题。 Also, the previous advice to init and alloc were necessary. 此外,以前的init和alloc建议是必要的。 Thanks everyone for the help! 谢谢大家的帮助!

For anyone else with this issue, there's another solution if you are planning on using MapKit . 对于有此问题的其他人,如果您计划使用MapKit ,还有另一种解决方案。

(The reason I say IF , of course, is because importing a module such as MapKit purely for a convenient wrapper method is probably not the best move.. but nonetheless here you go.) (当然,我说IF的原因是因为导入一个像MapKit这样的模块纯粹是为了一个方便的包装器方法可能不是最好的举动..但是在这里你去。)

@import MapKit;

Then just use MapKit's coordinate value wrapper whenever you need to: 然后只需在需要时使用MapKit的坐标值包装器:

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

In your example.. 在你的例子..

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

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

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