我的应用基于 MapKit,可以跟踪多个用户。现在使用我们的网络服务,我在地图上显示我的位置以及其他用户的最后一个位置,比如说 10 个位置。如果用户更新了他们的位置,它会通过网络服务发送并通过回调显示在地图上。我能够实时跟踪其他用户,但不知道如何在这里使用线程。我的 UI 有时会阻塞,有时还会由于内存问题而崩溃。
在我的connectionDidFinishLoading
方法中,我解析 JSON 数据,然后创建注释和覆盖:
-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSArray *trackingDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil];
NSLog(@"Json Dictionary = %@", trackingDict);
NSLog(@"COUNT = %i",trackingDict.count);
if ([trackingDict count] >= 2) {
for (trackUsersCount = 0; trackUsersCount< trackingDict.count; trackUsersCount++) {
NSLog(@"trackUsersCount %i", trackUsersCount);
NSMutableArray *latlongArray = [[NSMutableArray alloc]init];
latlongArray = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"latlong"];
[userLongitudeArray removeAllObjects];
[userLatitudeArray removeAllObjects];
for (int i = 0; i<latlongArray.count; i++) {
NSLog(@"COunt - > %@", [[latlongArray objectAtIndex:i]objectForKey:@"lat"]);
NSLog(@"COunt - > %@", [[latlongArray objectAtIndex:i]objectForKey:@"long"]);
[userLatitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"lat"]];
[userLongitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"long"]];
}
// ProfilePIC URL
profilePicURLString = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_profilePicture"];
NSString *name = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_firstName"];
[userNameArray addObject:name];
[profilePicURLStringArray addObject:profilePicURLString];
for (int i = 0; i<userLatitudeArray.count; i++) {
CLLocationCoordinate2D userLocation;
userLocation.latitude = [[userLatitudeArray objectAtIndex:i]doubleValue];
userLocation.longitude = [[userLongitudeArray objectAtIndex:i] doubleValue];
Annotation *Anno = [[Annotation alloc]init];
Anno.coordinate = userLocation;
Anno.title = name;
Anno.userProfileImageString = profilePicURLString;
[mapView addAnnotation:Anno];
}
NSLog(@"ARRAY for longitude %@", userLongitudeArray);
NSLog(@"ARRAY for latitude %@", userLatitudeArray);
int i;
for (i = 0; i<userLatitudeArray.count; i++) {
CLLocationCoordinate2D userLocation;
userLocation.latitude = [[userLatitudeArray objectAtIndex:i]doubleValue];
userLocation.longitude = [[userLongitudeArray objectAtIndex:i] doubleValue];
MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*userLongitudeArray.count);
pointsArray[i] = MKMapPointForCoordinate(userLocation);
polyline = [MKPolyline polylineWithPoints:pointsArray count:i];
free(pointsArray);
}
[mapView addOverlay:polyline];
}
}
[mapView reloadInputViews];
}
}
每 20 秒调用一次 Web 服务,我知道我可以在这里使用 GCD 或其他线程方法,但是在通过后台线程调用 Web 服务的时间间隔内,注释和覆盖不显示而不是地图。
任何帮助深表感谢!