我的服务器上有一个php文件,以这样的坐标格式输出每个用户的名字和位置:
user1: 34.208495,-83.411488
user2: 34.208434,-83.411443
user3: 34.208456,-83.411467
我用它来下载和解析文件:
NSString *myUrl = [NSString stringWithFormat:@"http://mysite.com/users/getlocations.php"];
NSURL *url = [NSURL URLWithString:myUrl];
[myWeb loadRequest:[NSURLRequest requestWithURL:url]];
NSString *string = [myWeb stringByEvaluatingJavaScriptFromString:@"document.documentElement.innerText"];
NSArray *components = [string componentsSeparatedByString:@"\n"];
[locationArray addObjectsFromArray:components];
这就是在地图上放一个图钉:
NSMutableArray* points = [[NSMutableArray alloc] initWithCapacity:100];
CLLocationDegrees latitude = lat;
CLLocationDegrees longitude = longi;
CLLocation* currentLocation = [[[CLLocation alloc] initWithLatitude:latitude longitude:longitude] autorelease];
[points addObject:currentLocation];
CSMapAnnotation* annotation = nil;
annotation = [[[CSMapAnnotation alloc] initWithCoordinate:[[points objectAtIndex:0] coordinate]
annotationType:CSMapAnnotationTypeImage
title:username] autorelease];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[mapView addAnnotation:annotation];
[UIView commitAnimations];
我基本上需要以一种方式解析文件,将用户名设置为引脚的标题,将坐标设置为引脚的坐标。
非常感谢!
答案 0 :(得分:0)
只是一个想法...
NSString *input = ...the list of user locations....
NSArray *locationArray = [input componentsSeparatedByString:@","];
但是,而不是这种格式: user3:34.208456,-83.411467
此方法需要以下格式:user3,34.208456,-83.411467,
另外,摆脱断裂线。基本上,您将生成CSV文件,然后创建数组中所有值的数组。不理想,但为了获得用户数量,例如,您可以写:
int numUsers = [locationArray count]/3.0f;
在地图上渲染所有这些位置,您只需解析数组以获取每个用户的数据。
但是,使用某种XML文件可能会更好,更有效。这样,您就不必一直将所有用户位置数据保存在内存中,并且解析起来会更容易。
编辑:
因此,例如,如果您想要三个字符串来表示给定用户的用户数据,您可以使用以下代码:
NSString *username;
float lat;
float lon;
/*
index represents the user from which you are grabbing data.
I'm arbitrarily setting the index to 5, meaning you would get data
for the 5th user listed.
*/
int index = 5;
username = [locationArray objectAtIndex:3*index];
lat = [[locationArray objectAtIndex:3*index+1] floatValue];
lon = [[locationArray objectAtIndex:3*index+2] floatValue];