2012-08-14 21 views
2

如何在NSMutableArray中存储多个点(经度和纬度)?在NSMutableArray中存储经度和纬度Xcode

+0

请搜索,发布问题之前。这是一种创建二维数组的方法http://stackoverflow.com/questions/724772/2d-arrays-in-objective-c – 2012-08-14 11:02:29

+0

@SharonNathaniel这不是一个关于二维数组的问题。经度和纬度不是整数,它们不能用作数组的指示。 – joerick 2012-08-14 11:28:59

回答

1
//NSMutable array to store values 
NSMutableArray *array =[[NSMutableArray alloc] init];  
//some lat and long values 
NSDictionary *latLongDict = @{@"lat" : @(18.25689), @"long":@(48.25689)}; 

//add the objects to the array 
[array addObject:latLongDict]; 
+0

如果你要这样做,NSNumber会更合适。但在我看来,把这个对作为单独的实体存储在数组中是没有意义的。 – joerick 2012-08-14 11:26:31

6

你有几种选择:

我推荐后者,因为你可能会发现你需要CLLocation la上的额外功能之三。

-1

使用Mapkit框架,您可以通过位置与它们的纬度找到附近沿着当前的位置&经度上阵列

CLLocationCoordinate2D location; 
location = [mMapView.userLocation coordinate]; 

if(iLat && iLng) 
{ 

    location.latitude = [iLat floatValue]; 
    location.longitude = [iLng floatValue]; 
} 

储值

NSMutableArray *a= [[NSMutableArray alloc] initWithObjects:location.latitude,location.longitude, nil]; 
+0

纬度和经度是双打的,而不是对象。 – joerick 2012-08-14 11:29:32

+0

而你的第一个例子没有意义。这是什么? – joerick 2012-08-14 11:31:20

0

你可以存储中的数据字典对象:

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

//some lat and long values 
CGFloat latitude = 18.25689; 
CGFloat longitude = 48.25689; 

NSDictionary *locationDict = @{ @"latitude" : [NSNumber numberWithFloat:latitude], @"longitude" : [NSNumber numberWithFloat:longitude] }; 

[locationArray addObject:locationDict]; 

,并通过访问它们:

NSUInteger anIndex = 0; 
NSDictionary *locDict = [locationArray objectAtIndex:anIndex]; 
NSNumber *latitudeNumber = (NSNumber *)[locDict objectForKey:@"latitude"]; 
NSNumber *longitudeNumber = (NSNumber *)[locDict objectForKey:@"longitude"];