2012-03-27 60 views
-1

我有一个字符串值。如何解析以下字符串

NSString *myString = @"Latitude:-31.9504140#Longitude:115.8606040"; 

我怎样才能检索到字符串中的纬度和经度值并指定为协调

CLLocationCoordinate2D coordinate; 
coordinate.Latitude; 
coordinate.Longitude; 
+0

你的弦甲酸盐每次都是一样的.. – Narayana 2012-03-27 06:21:09

+0

是的。也许稍微改变像31.9504140 – 2012-03-27 06:24:12

+0

这样的值,然后用我的答案,它会帮助你 – Narayana 2012-03-27 06:30:00

回答

6

如果您的格式是相同的,那么你可以经纬度使用下面的代码获得

NSString *myString = @"Latitude:-31.9504140#Longitude:115.8606040"; 
NSString *Latitude = [[[[myString componentsSeparatedByString:@"#"] objectAtIndex:0] componentsSeparatedByString:@":"] objectAtIndex:1]; 
NSString *Longitude = [[[[myString componentsSeparatedByString:@"#"] objectAtIndex:1] componentsSeparatedByString:@":"] objectAtIndex:1]; 
NSLog(@"Latitude--%@,Longitude---%@",Latitude,Longitude); 
1

像这样的事情应该这样做:

NSArray *components = [myString componentsSeparatedByString:@"#"]; 
NSArray *latitude = [[components objectAtIndex:0] componentsSeparatedByString:@":"]; 
NSArray *longitude = [[components objectAtIndex:1] componentsSeparatedByString:@":"]; 

CLLocationCoordinate2D coordinate; 
coordinate.Latitude = [[latitude objectAtIndex:1] doubleValue]; 
coordinate.Longitude = [[longitude objectAtIndex:1] doubleValue]; 
6
double lon, lat; 
sscanf([myString UTF8String], "Latitude:%lf#Longitude:%lf", &lon, &lat); 
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lon, lat); 
+0

有趣的方法:) – Vignesh 2012-03-27 06:50:32