Advertisement
Guest User

Untitled

a guest
Oct 7th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)searchBarDidTapReturn:(INSSearchBar *)searchBar{
  2.    
  3.     NSString *searchText = searchBar.searchField.text;
  4.    
  5.     CLLocationCoordinate2D currentCoordinates = [[LocationManager sharedInstance] getCurrentLocation];
  6.  
  7.     NSNumber *lat = [[NSNumber alloc] initWithDouble:currentCoordinates.latitude];
  8.     NSNumber *lon = [[NSNumber alloc] initWithDouble:currentCoordinates.longitude];
  9.    
  10.     [GooglePlaces nearbySearch:searchText latitude:lat longitude:lon radius:@(10000) callback:^(BOOL success, id result) {
  11.         if (success) {
  12.             NSDictionary *json = (NSDictionary *) result;
  13.             NSArray *resultObj = [json valueForKey:@"results"];
  14.            
  15.             for(NSDictionary *object in resultObj){
  16.            
  17.                  LocationModel *model = [[LocationModel alloc] init];
  18.                  model.placeName = [object valueForKey:@"name"];
  19.                  model.locAddress = [object objectForKey:@"vicinity"];
  20.                  NSArray *photos = [object valueForKey:@"photos"];
  21.                 // GET PHOTO URL
  22.                 if(photos.count > 0 ){
  23.                     NSDictionary *photoObj = [photos objectAtIndex:0];
  24.                     NSArray *attribArray = [photoObj valueForKey:@"html_attributions"];
  25.                     if(attribArray.count > 0){
  26.                        
  27.                         model.locUrl = [attribArray objectAtIndex:0];
  28.                     }
  29.                 }
  30.                
  31.                 // GET DRIVING DISTANCE \\
  32.                 // ******************** \\
  33.                
  34.                 NSDictionary *coordinates = [[object objectForKey:@"geometry"] objectForKey:@"location"];
  35.                
  36.                 NSNumber * latitude =  [NSNumber numberWithDouble:[[coordinates valueForKeyPath:@"lat"]doubleValue]];
  37.                 NSNumber * longitude =  [NSNumber numberWithDouble:[[coordinates valueForKeyPath:@"lng"]doubleValue]];
  38.                
  39.                 // GET LATITUDE LONGITUDE OF CURRENT LOCATRION
  40.                 NSString *preLat = [lat stringValue];
  41.                 NSString *lng = [lon stringValue];
  42.                 NSString *postLat = [preLat stringByAppendingString:@","];
  43.                 NSString *originCoordinates = [postLat stringByAppendingString:lng];
  44.                
  45.                
  46.                
  47.                 [GooglePlaces getDistance:originCoordinates latitude:latitude longitude:longitude callback:^(BOOL success, id result) {
  48.                     if (success) {
  49.                         NSDictionary *json = (NSDictionary *) result;
  50.                        
  51.                         NSArray *rows = [json valueForKey:@"rows"];
  52.                         NSDictionary *elements = [rows valueForKey:@"elements"];
  53.                         NSArray *distance = [elements valueForKey:@"distance"];
  54.                         NSDictionary *value = [distance objectAtIndex:0];
  55.                         NSArray *dist = [value valueForKey:@"value"];
  56.                         NSNumber *distFromArray = [dist objectAtIndex:0];
  57.                        
  58.                        
  59.                         double calc = (distFromArray.doubleValue / 1609.34 );
  60.                         NSNumber *miles = [NSNumber numberWithDouble:calc];
  61.                         NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  62.                         [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  63.                         [formatter setMaximumFractionDigits:2];
  64.                         [formatter setRoundingMode: NSNumberFormatterRoundUp];
  65.                         NSString *temp = [formatter stringFromNumber:miles];
  66.                         NSNumber *roundedDistance = [formatter numberFromString:temp];
  67.                         model.distance = roundedDistance;
  68.                         [self.searchArray addObject:model];
  69.                         NSLog(@"Search array count %lu", self.searchArray.count);
  70.                     }
  71.                 }];
  72.                
  73.             }
  74.            
  75.              [self.tableView reloadData];
  76.         }
  77.     }];
  78.    
  79.    
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement