我的应用程序是实时跟踪器,多个用户登录并通过将他们的坐标发送到我们的网络服务来更新他们的位置,然后每 2 分钟调用一次,让我们在我的MapView
.
每次我从 Web 服务中获取用户的位置时connectionDidFinishLoading
,我都会解析、创建并将它们添加polyline
到:pointsArray
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];
}
}
}
}
我想要做的是控制polyline
为每个用户创建的每一个,所以我可以更改它的颜色并通过单击一个按钮来隐藏/显示它们(一个显示/隐藏我的曲目,另一个显示/隐藏我的曲目和其他所有用户) ,这就是为什么我要为其添加标题。我现在可以看到我正在添加polyline
相同的overlay
内容,我认为这是错误的。但我不知道网络服务中会有多少用户,所以可以添加它们的多个叠加层。
最初我以为我可以删除polyline
带有标题的特定内容,但后来我意识到随着polyline.title
属性的更新,它正在删除所有内容。
任何帮助将非常感激!