问题描述
我正在尝试获取我存储在parse.com中的纬度和经度双值数组。我希望能够将这些转换回CLLocationCoordinates,然后我可以使用它在自定义表视图中创建MKPloylines。
但是我不太确定如何存储一系列CLLocationCoordinates。
这是我的代码:
I'm attempting to get an array of latitude and longitude double values which I have stored in parse.com. I want to be able to convert these back into CLLocationCoordinates which I can then use to create MKPloylines in a custom table view. However I'm not too sure how I can store an array of CLLocationCoordinates. Here is my code:
_latitudeArray = [object objectForKey:@"latitude"];
[_parseLatitudeArray addObject:_latitudeArray];
_longitudeArray = [object objectForKey:@"longitude"];
[_parseLongitudeArray addObject:_longitudeArray];
for (int i=0; i < [_parseLatitudeArray count]; i++) {
_latitude = [self.parseLatitudeArray objectAtIndex:i];
_longitude = [self.parseLongitudeArray objectAtIndex:i];
CLLocationCoordinate2D coordinates [[_latitude count]];
for (int i = 0; i < [_latitude count]; i++) {
CLLocationCoordinate2D coordinate;
double latitude = [[_latitude objectAtIndex:i] doubleValue];
double longitude = [[_longitude objectAtIndex:i] doubleValue];
coordinate.latitude = latitude;
coordinate.longitude = longitude;
coordinates[i] = coordinate;
}
[_coordinatesArray addObject:coordinates];
}
NSLog(@"coordinates array = %@", _coordinatesArray);
起初我想我可以用[_coordinatesArray addObject:coordinates]做到这一点。但后来意识到这样做不行!
At first I figured I would be able to do it using [_coordinatesArray addObject:coordinates]; but then realised that would not work!
我可能会遗漏一些非常基本的东西,但任何帮助都会受到赞赏。
谢谢
I may be missing something really basic but any help would be appreciated. Thanks
编辑:
属性定义:
property Definitions:
@property NSMutableArray *latitudeArray;
@property NSMutableArray *parseLatitudeArray;
@property NSMutableArray *longitudeArray;
@property NSMutableArray *parseLongitudeArray;
@property NSMutableArray *coordinatesArray;
@property NSArray *latitude;
@property NSArray *longitude;
@property CLLocationCoordinate2D *coordinates;
推荐答案
而不是创建结构的C数组,换行将每个结构体转换为一个对象并将其添加到NSMutableArray中。因此,在创建坐标后,只需:
Instead of creating a C-array of structs, wrap each struct into an object and add it to an NSMutableArray. So after you create coordinate, just:
[coordinatesArray addObject: [NSValue valueWithMKCoordinate:coordinate]];
并且要取回价值:
CLLocationCoordinate2D coordinate;
[coordinatesArray[i] getValue:&coordinate];
因此,假设self.latitude和self.longitude是NSStrings的NSArrays:
So, assuming that self.latitude and self.longitude are NSArrays of NSStrings:
//only property is now
@property (nonatomic, strong) NSArray * locations
-(void) loadCoordinatesFromParse {
NSDictionary * parseData;
//load parseData from Parse here
NSMutableArray * coordinates = [NSMutableArray array];
NSArray * latitudes = [parseData objectForKey:@"latitude"];
NSArray *longitudes = [parseData objectForKey:@"longitude"];
for (int i = 0; i < [latitudes count]; i++) {
double latitude = [latitudes[i] doubleValue];
double longitude = [longitudes[i] doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[coordinates addObject: [NSValue valueWithMKCoordinate:coordinate]];
}
NSLog(@"coordinates array = %@", coordinates);
self.locations = [NSArray arrayWithArray: coordinates];
}
这篇关于创建CLLocationCoordinates数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!