我的应用程序是实时跟踪器,其中多个用户登录并通过将其坐标发送到我们的Web服务来更新其位置,然后每2分钟调用一次,让我们在MapView上显示所有用户。

每次我使用connectionDidFinishLoading方法从Web服务获取用户位置时,我都会进行解析,通过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中,我相信这是错误的。但是我不知道Web服务中将有多少用户,因此可以为其添加多个覆盖。

最初我以为我可以删除带有标题的特定polyline,但后来我意识到随着polyline.title属性的更新,它可以删除所有内容。

任何帮助将非常感激!

最佳答案

您可以收集与其他用户相关的这些跟踪的数组,并为当前用户保留一个跟踪。如果您在connectionDidFinishLoading函数的开头清理数组并将其填充到当前将叠加层添加到 map 的位置,则可以将addOverlay移动到末尾调用的新函数。

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

您也可以在按下按钮且状态改变时调用它。

关于iphone - 从通过Web服务到达的位置创建多个折线的MKOverlay,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14640256/

10-12 21:27