Updating, Managing, and Displaying User Locations


/ Published in: Objective C
Save to your folder(s)

How to manage and update user locations using the onUnityLocations delegate.


Copy this code and paste it in your HTML
  1. - (void) onUnityLocations:(NSDictionary *)locations
  2. {
  3.  
  4. for(NSNumber *key in [locations allKeys])
  5. {
  6. ProfileLocation *affiliateLocation = [locations objectForKey:key];
  7.  
  8. if(![self.unityAffiliateMarkers objectForKey:key]) // the users profile and location has not been cached yet
  9. {
  10. Profile *affiliateProfile = [[Profile alloc]init];
  11. affiliateProfile.entityId = affiliateLocation.profileId;
  12. if(!affiliateProfile.entityId==0)
  13. {
  14. [self readUnityObject:affiliateProfile withCompletionBlock:^(id object, bool success, NSString * errorMessage)
  15. {
  16. if(success) // generate a GMSMarker containing the profile object
  17. {
  18. GMSMarker *affiliateLocationMarker = [GMSMarker markerWithPosition:CLLocationCoordinate2DMake(affiliateLocation.latitude, affiliateLocation.longitude)];
  19. affiliateLocationMarker.flat = YES;
  20. affiliateLocationMarker.zIndex = -1;
  21. affiliateLocationMarker.appearAnimation=kGMSMarkerAnimationPop;
  22. affiliateLocationMarker.groundAnchor = CGPointMake(0.5, 0.5);
  23. [affiliateLocationMarker setUserData:((Profile *)object)]; // this sets the profile to the marker
  24. affiliateLocationMarker.icon= [TenDegreesHelpers roundImageCorners:[self applyAlpha:1 toImage:[self image:[TenDegreesHelpers stringToUIImage:((Profile *)object).icon] scaledToSize:CGSizeMake(30.0f, 30.0f)]]];
  25.  
  26. if(![self.unityAffiliateMarkers objectForKey:key]) // check again to prevent duplicate locations
  27. {
  28. affiliateLocationMarker.map=self.googleMap;
  29. [self.unityAffiliateMarkers setObject:affiliateLocationMarker forKey:key];
  30. }
  31. }
  32. else
  33. {
  34. NSLog(@"%@",errorMessage);
  35. }
  36. }];
  37. }
  38.  
  39. }
  40. else // users location and profile are already cached - just update their location on the map
  41. {
  42. GMSMarker *affiliateLocationMarker = [self.unityAffiliateMarkers objectForKey:key];
  43. [affiliateLocationMarker setPosition:CLLocationCoordinate2DMake(affiliateLocation.latitude, affiliateLocation.longitude)];
  44.  
  45. }
  46.  
  47. }
  48. for(NSNumber *key in [self.unityAffiliateMarkers allKeys])
  49. {
  50. if(![locations objectForKey:key]) // remove any markers of users who have logged out or timed out
  51. {
  52. GMSMarker *affiliateLocationMarker = [self.unityAffiliateMarkers objectForKey:key];
  53. affiliateLocationMarker.map=nil;
  54. [self.unityAffiliateMarkers removeObjectForKey:key];
  55. }
  56. }
  57. [_extendUpdateDelegate onLocations:locations];
  58. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.