Tìm vị trí

Vietbando hỗ trợ thông tin tìm kiếm vị trí đa dạng như: Tìm địa chỉ, Tìm bản đồ hành chính các cấp, Tìm tên đường, tìm giao lộ, Tìm địa danh, Tìm địa điểm du lịch, Tìm điểm dịch vụ.

Tải về thư viện Vietbando Services.

#import "ViewController.h"

#define KEY_SEARCH  @"Hotel"

@import Vietbando;
@import VietbandoServices;

@interface ViewController ()
@property (nonatomic) IBOutlet VBDMapView *mapView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewWillAppear:(BOOL)animated{
    
    if ([VBDAccountManager accessToken].length == 0)
    {
        UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"Register Key" message:@"Enter your Vietbando Register Key:" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField)
         {
             textField.keyboardType = UIKeyboardTypeURL;
             textField.autocorrectionType = UITextAutocorrectionTypeNo;
             textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
         }];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
        UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
                                   {
                                       UITextField *textField = alertController.textFields.firstObject;
                                       NSString *accessToken = textField.text;
                                       [VBDAccountManager setAccessToken:accessToken];
                                       
                                       [self.mapView reloadStyle:self];
                                       [self Search:KEY_SEARCH];
                                   }];
        
        [alertController addAction:OKAction];
        [self presentViewController:alertController animated:YES completion:nil];
    }
    else{
        [self Search:KEY_SEARCH];
    }
}

- (void) Search:(NSString *)key{
    
    CLLocationCoordinate2D lefttop = [self.mapView convertPoint:CGPointMake(0, 0) toCoordinateFromView:self.mapView];
    CLLocationCoordinate2D rightbottom = [self.mapView convertPoint:CGPointMake(self.mapView.bounds.size.width, self.mapView.bounds.size.height) toCoordinateFromView:self.mapView];
    NSString *sRegisterKey = [VBDAccountManager accessToken];
    
    [VBDServices Search:key page:[NSNumber numberWithInt:1] pagesize:[NSNumber numberWithInt:10] lefttop:lefttop rightbottom:rightbottom registerKey:sRegisterKey success:^(id obj) {
            NSArray *list = (NSArray *)obj;
            if( list && [list isKindOfClass:[NSArray class]] && [list count]>0 ){
                NSMutableArray *annos = [[NSMutableArray alloc] init];
                for( NSDictionary *dic in list )
                {
                    VBDPointAnnotation *anno = [[VBDPointAnnotation alloc] init];
                    double lat = [[dic objectForKey:@"Latitude"] doubleValue];
                    double lon = [[dic objectForKey:@"Longitude"] doubleValue];
                    anno.coordinate = CLLocationCoordinate2DMake(lat,lon);
                    anno.title = [dic objectForKey:@"Name"];
                    [annos addObject:anno];
                }
                [self.mapView addAnnotations:annos];
                [self.mapView showAnnotations:annos animated:YES];
            }
            else{
                [self showError:@"Không tìm thấy!"];
            }
        }
       fail:^(id obj) {
           [self showError:(NSString *)obj];
       }
     ];
}

- (void) showError:(NSString *)errorText{
    UIAlertController*alertController = [UIAlertControlleralertControllerWithTitle:@"Error" message:errorText preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:ok];
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark VBDMapView
// Use the default marker; see our custom marker example for more information
- (VBDAnnotationImage *)mapView:(VBDMapView *)mapView imageForAnnotation:(id )annotation {
    return nil;
}

// Allow markers callouts to show when tapped
- (BOOL)mapView:(VBDMapView *)mapView annotationCanShowCallout:(id )annotation {
    return YES;
}
@end