我该如何解析以下字符串

时间:2012-03-27 06:14:58

标签: iphone objective-c nsstring coordinates

我有一个字符串值。

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

如何从字符串中检索纬度和经度值并将其指定给坐标

CLLocationCoordinate2D coordinate;
coordinate.Latitude;
coordinate.Longitude;

3 个答案:

答案 0 :(得分: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 :(得分:6)

double lon, lat;
sscanf([myString UTF8String], "Latitude:%lf#Longitude:%lf", &lon, &lat);
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(lon, lat);

答案 2 :(得分: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];