【问题标题】:Store latitude and longitude in NSMutableArray Xcode在 NSMutableArray Xcode 中存储经纬度
【发布时间】:2012-08-14 10:54:17
【问题描述】:

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

【问题讨论】:

标签: iphone objective-c ios xcode ipad


【解决方案1】:

你有几个选择:

我推荐后者,因为您可能会发现稍后需要 CLLocation 上的额外功能。

【讨论】:

    【解决方案2】:
    //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];
    

    【讨论】:

    • NSNumber 如果你这样做会更合适。但在我看来,将这对作为单独的实体存储在数组中是没有意义的。
    【解决方案3】:

    您可以将数据存储在字典对象中:

    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"];
    

    【讨论】:

      【解决方案4】:

      使用 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];
      

      【讨论】:

      • 纬度和经度是双精度的,不是对象。
      • 你的第一个例子没有意义。这是什么?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-03
      • 2010-11-16
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多