0

My app is real-time tracker, where multiple users are logged in and updating their location by sending their co-ordinates to our web service which is then called back let's after every 2 minutes to show all the users on my MapView.

Every time I get the locations of users from web service in connectionDidFinishLoading method, I am parsing, creating polyline through pointsArray and adding them to 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];
        }
   }
  }
}

What I want to do is to have control on each polyline created for each user, so I can change the color of it and hide/show them on click of a button (one to show/hide my track and the other for all other users), this is why I am adding title to it. I can see now that I am adding polyline to the same overlay, which is wrong I believe. But I don't know how many users will be there in web-service so can add multiple overlays of them.

Initially I thought I am able to remove a particular polyline with title but then I realised it is removing all as polyline.title property gets updated.

Any help would be much appreciated!

1 Answer 1

1

You could collect an array of those tracks that relate to other users, and keep a single track for the current user. If you clean the array at the start of the connectionDidFinishLoading function and populate it where you are currently adding the overlays to the map, then you move the addOverlay to a new function that you call at the end.

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

You can also call this when ever the button is pressed and the state changes.

6
  • thanks I got that, but the problem here is if I want to control the color of polylines for each user I won't be able to do it unless I add separate overlay for each user, am I right? Feb 1, 2013 at 8:51
  • Correct, and that's why otherUserTracks is an array of ploylines. Then in viewForOverlay you'll need to determine which is which and colour them accordingly.
    – Craig
    Feb 1, 2013 at 20:18
  • is it possible to control color of each polyline in array, but within the same overlay? Please provide some sample, as I am confused how to do this when I have to show multiple polylines of different color in otherUserTracks? Feb 2, 2013 at 16:57
  • The trick is they are not separate overlays. You add the array using MKMapView addOverlays. Then when viewForOverlay gets called it will be called for each item in that array, i.e. each ployline.
    – Craig
    Feb 2, 2013 at 23:21
  • Thanks Craig, I hope I am not being pain in the back, but I think I am doing in the same way as you suggested, getting array, creating polyline and calling mapView addOverlay:polyline which will call viewForOverlay delegate method, the problem here is every time this method is called, it is overriding value of 'polyline' which is declared global and thus showing property color and the title which was defined last in the loop. What could be the optimized solution for this? Feb 5, 2013 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.