-5

我有一个 NSString,其值为:

NSString *combined = "10014, 40.734, -74.0053";

我想将这个有逗号的字符串分成三个 NSString,分别称为 bZip、bLat 和 bLon,以便值如下所示:

bZip = 10014 
bLat = 40.734 
bLon = -74.0053

然后我想使用 bLat 和 bLon 坐标来更新 MapView 上的中心位置。MapView 从 JSON 提要中提取数据并绘制标记,但我需要将地图重新​​聚焦在保存在 bLat 和 bLon 值中的坐标上。

我假设我需要在该行之后进行此更改:

[self.mapView1 addAnnotations:newAnnotations];

有谁知道如何帮助我实现这一目标?谢谢你们!

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:0
                                                   error:&error];

CLLocationCoordinate2D location;                        
NSMutableArray *newAnnotations = [NSMutableArray array]; 
MapViewAnnotation *newAnnotation;                        

for (NSDictionary *dictionary in array)
{   
    location.latitude = [dictionary[@"placeLatitude"] doubleValue];
    location.longitude = [dictionary[@"placeLongitude"] doubleValue];

    newAnnotation = [[MapViewAnnotation alloc] initWithTitle:dictionary[@"name"]
                                               andCoordinate:location];

    [newAnnotations addObject:newAnnotation];


}

[self.mapView1 addAnnotations:newAnnotations];
4

3 回答 3

3

示例代码:

NSArray *stringArray = [combined componentsSeparatedByString: @","];

NSString *bZip = [stringArray objectAtIndex:0];
NSString *bLat = [stringArray objectAtIndex:1];
NSString *bLon = [stringArray objectAtIndex:2];
于 2013-06-11T07:36:55.977 回答
2
 NSString *combined = @"10014, 40.734, -74.0053";
 NSArray *array = [combined componentsSeparatedByString:@","];
于 2013-06-11T07:39:38.817 回答
1

尝试使用这个

    NSString *combined = "10014, 40.734, -74.0053";
    NSArray *data = [combined componentsSeparatedByString:@", "];

NSLog(@"bZip = %@",[data objectAtIndex:0]);
NSLog(@"bLat = %@",[data objectAtIndex:1]);
NSLog(@"bLon = %@",[data objectAtIndex:2]);
于 2013-06-11T07:37:07.830 回答