Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)searchBarDidTapReturn:(INSSearchBar *)searchBar{
- NSString *searchText = searchBar.searchField.text;
- CLLocationCoordinate2D currentCoordinates = [[LocationManager sharedInstance] getCurrentLocation];
- NSNumber *lat = [[NSNumber alloc] initWithDouble:currentCoordinates.latitude];
- NSNumber *lon = [[NSNumber alloc] initWithDouble:currentCoordinates.longitude];
- [GooglePlaces nearbySearch:searchText latitude:lat longitude:lon radius:@(10000) callback:^(BOOL success, id result) {
- if (success) {
- NSDictionary *json = (NSDictionary *) result;
- NSArray *resultObj = [json valueForKey:@"results"];
- for(NSDictionary *object in resultObj){
- LocationModel *model = [[LocationModel alloc] init];
- model.placeName = [object valueForKey:@"name"];
- model.locAddress = [object objectForKey:@"vicinity"];
- NSArray *photos = [object valueForKey:@"photos"];
- // GET PHOTO URL
- if(photos.count > 0 ){
- NSDictionary *photoObj = [photos objectAtIndex:0];
- NSArray *attribArray = [photoObj valueForKey:@"html_attributions"];
- if(attribArray.count > 0){
- model.locUrl = [attribArray objectAtIndex:0];
- }
- }
- // GET DRIVING DISTANCE \\
- // ******************** \\
- NSDictionary *coordinates = [[object objectForKey:@"geometry"] objectForKey:@"location"];
- NSNumber * latitude = [NSNumber numberWithDouble:[[coordinates valueForKeyPath:@"lat"]doubleValue]];
- NSNumber * longitude = [NSNumber numberWithDouble:[[coordinates valueForKeyPath:@"lng"]doubleValue]];
- // GET LATITUDE LONGITUDE OF CURRENT LOCATRION
- NSString *preLat = [lat stringValue];
- NSString *lng = [lon stringValue];
- NSString *postLat = [preLat stringByAppendingString:@","];
- NSString *originCoordinates = [postLat stringByAppendingString:lng];
- [GooglePlaces getDistance:originCoordinates latitude:latitude longitude:longitude callback:^(BOOL success, id result) {
- if (success) {
- NSDictionary *json = (NSDictionary *) result;
- NSArray *rows = [json valueForKey:@"rows"];
- NSDictionary *elements = [rows valueForKey:@"elements"];
- NSArray *distance = [elements valueForKey:@"distance"];
- NSDictionary *value = [distance objectAtIndex:0];
- NSArray *dist = [value valueForKey:@"value"];
- NSNumber *distFromArray = [dist objectAtIndex:0];
- double calc = (distFromArray.doubleValue / 1609.34 );
- NSNumber *miles = [NSNumber numberWithDouble:calc];
- NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
- [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
- [formatter setMaximumFractionDigits:2];
- [formatter setRoundingMode: NSNumberFormatterRoundUp];
- NSString *temp = [formatter stringFromNumber:miles];
- NSNumber *roundedDistance = [formatter numberFromString:temp];
- model.distance = roundedDistance;
- [self.searchArray addObject:model];
- NSLog(@"Search array count %lu", self.searchArray.count);
- }
- }];
- }
- [self.tableView reloadData];
- }
- }];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement