我有一个NSString,其值为:
NSString *combined = "10014, 40.734, -74.0053";
我想打破这个字符串,其中有逗号分为三个NSStrings,它们调用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];
答案 0 :(得分:3)
示例代码:
NSArray *stringArray = [combined componentsSeparatedByString: @","];
NSString *bZip = [stringArray objectAtIndex:0];
NSString *bLat = [stringArray objectAtIndex:1];
NSString *bLon = [stringArray objectAtIndex:2];
答案 1 :(得分:2)
NSString *combined = @"10014, 40.734, -74.0053";
NSArray *array = [combined componentsSeparatedByString:@","];
答案 2 :(得分: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]);