I have a php file on my server that outputs each user’s name and location, in coordinate format like this:
user1: 34.208495,-83.411488
user2: 34.208434,-83.411443
user3: 34.208456,-83.411467
I am using this to download and parse the file:
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];
And this to put a pin on the map:
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];
I basically need to parse the file in a way that sets the username as the pin’s title and the coordinates as the pin’s coordinates.
Thanks a lot!
Just a thought…
instead of this format, however:
user3: 34.208456,-83.411467
this method requires this format: user3, 34.208456,-83.411467,
Also, get rid of the breaklines. Basically, you would be generating a CSV file, and then creating an array of all the values in the array. Not ideal, but to get the number of users, for example, you could write:
The render all these positions on a map, you would just parse through the array to grab the data for each user.
It would probably be better and more efficient to use some sort of XML file, though. That way you wouldn’t necessarily have to keep all the user location data in memory at all times, and it would be easier to parse.
EDIT:
so, for example, if you wanted three strings representing the user data for a give user, you might use code like this: