通过networking服务创build多个折线的MKOverlay

我的应用程序是实时跟踪器,多用户login和更新他们的位置,通过发送他们的坐标到我们的Web服务,然后callback,让我们每2分钟后显示所有用户在我的MapView

每次我从web服务中获取用户在connectionDidFinishLoading方法中的位置时,我正在parsing,通过pointsArray创build多段polyline并将它们添加到overlay

 -(void) connectionDidFinishLoading: (NSURLConnection *) connection { userLatitudeArray = [[NSMutableArray alloc]init]; userLongitudeArray = [[NSMutableArray alloc]init]; userIdArray = [[NSMutableArray alloc]init]; userNameArray = [[NSMutableArray alloc]init]; userProfilePicArray = [[NSMutableArray alloc]init]; profilePicURLStringArray = [[NSMutableArray alloc]init]; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; NSArray *trackingDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil]; 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++) { [userLatitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"lat"]]; [userLongitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"long"]]; } NSString *name = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_firstName"]; // ProfilePIC URL profilePicURLString = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_profilePicture"]; [userNameArray addObject:name]; [profilePicURLStringArray addObject:profilePicURLString]; int i; if (userLatitudeArray.count>1) { 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); } polyline.title = name; [mapView addOverlay:polyline]; } } } } 

我想要做的就是控制每个用户创build的每条多段polyline ,所以我可以改变它的颜色,并单击button来隐藏/显示它们(一个显示/隐藏我的轨道,另一个显示其他用户),这就是为什么我要添加标题。 我现在可以看到,我正在添加polyline到相同的overlay ,这是我相信是错误的。 但是我不知道web服务中会有多less用户,所以可以添加多个用户。

最初我以为我能够删除一个特定的polyline与标题,但后来我意识到这是删除所有polyline.title属性得到更新。

任何帮助将非常感激!

您可以收集与其他用户相关的一系列曲目,并为当前用户保留单曲。 如果在connectionDidFinishLoading函数开始时清理数组,并将其填充到当前将地图叠加到地图上的位置,则将addOverlay移动到您在最后调用的新函数。

 - (void) resetMap { if (showOtherTracks) { [mapView addOverlays:otherUserTracks]; } else { [mapView removeOverlays:otherUserTracks]; } if (showMyTrack) { [mapView addOverlay:myTrack]; } else { [mapView removeOverlay:myTrack]; } } 

当button被按下并且状态改变时,您也可以调用它。