Load Places Automatically By Using Google Maps

Loading Places By Typing.............

*First Install Pods.
*After Installing Pods, Proceed For Below

#import <GoogleMaps/GoogleMaps.h>
#pragma mark- Google Api
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
    
    txtState.text=@"";
    txtZip.text=@"";
    txtCity.text=@"";
    //IPHONE
    _stateTFIPhone.text=@"";
    _zipTFIPhone.text=@"";
    _cityTFIPhone.text=@"";
    
    // Do something with the selected place.
    NSLog(@"Place name %@", place.name);
    NSLog(@"Place address %@", place.formattedAddress);
    NSLog(@"Place attributions %@", place.attributions.string);
    NSLog(@"lat and log%f", place.coordinate.latitude);
    NSLog(@"lang %f", place.coordinate.longitude);
    NSString *locationString =place.name;
    NSString *url = [NSString stringWithFormat:@"Your API with Parameters", locationString];
    url = [url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    
    NSURL *wurl = [NSURL URLWithString:url];
    NSData *data = [NSData dataWithContentsOfURL: wurl];
    NSMutableArray*listArray=[NSMutableArray new];
    // Fail to get data from server
    if (nil == data) {
        
        NSLog(@"Error: Fail to get data");
    }
    else{
        // Parse the json data
        NSError *error;
        NSDictionary *json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:kNilOptions
                              error:&error];
        
        // Check status of result
        NSString *resultStatus = [json valueForKey:@"status"];
        
        // If responce is valid
        if ([resultStatus isEqualToString:@"OK"] )
        {
            
            NSLog(@" data of location :%@", json);
           listArray=[[[json objectForKey:@"results"]objectAtIndex:0]objectForKey:@"address_components"];
            
        }
       
    }
    for (int i=0; i<listArray.count; i++)
    {
        NSArray *names = [[listArray objectAtIndex:i]objectForKey:@"types"];
        
        for (NSString*stg in names)
        {
            NSLog (@"%@",stg);
        
        if ([stg isEqualToString:@"administrative_area_level_1"])
        {
            NSString*stateStg=[[listArray objectAtIndex:i]objectForKey:@"short_name"];
            txtState.text=stateStg;
            _stateTFIPhone.text=stateStg;
        }
        }
    }
   
    GMSPlacesClient *placesClient = [[GMSPlacesClient allocinit];
    
    [placesClient lookUpPlaceID:place.placeID callback:^(GMSPlace *place, NSError *error) {
        if (error != nil) {
            NSLog(@"Place Details error %@", [error localizedDescription]);
            return;
        }
        if (place != nil) {
            NSString *locality = @"";
            txtAddress.text=@"";
          _address1TFIPhone.text=@"";
           // txtCustAddress1Iphone.text=@"";
            for (GMSAddressComponent *add in place.addressComponents) {
                //locality
                NSLog(@" data of list %@",add.type);
                NSLog(@"%@",add.name);
            
                
                    if ([add.type isEqualToString:@"street_number"] || [add.type isEqualToString:@"route"] || [add.typeisEqualToString:@"street_number"] || [add.type isEqualToString:@"street_number"]) {
                        txtAddress.text=[NSString stringWithFormat:@"%@ %@",txtAddress.text,add.name];
                         _address1TFIPhone.text=[NSString stringWithFormat:@"%@ %@",txtAddress.text,add.name];
                    }
                    if ([add.type isEqualToString:@"locality"]) {
                        txtCity.text=add.name;
                        _cityTFIPhone.text=add.name;
                    }
//                    if ([add.type isEqualToString:@"administrative_area_level_1"]) {
//                        txtState.text=add.name;
//                        
//                    }
               
                    if ([add.type isEqualToString:@"postal_code"]) {
                        txtZip.text=add.name;
                        _zipTFIPhone.text=add.name;
                    }
                //type will be street_name, locality, admin_area, country,
                //name will hold the corresponding value
            }
            
        else {
            NSLog(@"No place details for %@", place.placeID);
        }
        
    }];
    
    if (txtAddress.text.length <= 0)
    {
        txtAddress.placeholder=@" Address";
    }
    txtPhone.text=place.phoneNumber;
    [activeField resignFirstResponder];
    
    acController.view.hidden=true;
    [acController.view removeFromSuperview];
}

- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
    // TODO: handle the error.
    NSLog(@"error: %ld", (long)[error code]);
    acController.view.hidden=true;
    [acController.view removeFromSuperview];
    [activeField resignFirstResponder];
    
}

// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
    NSLog(@"Autocomplete was cancelled.");
    acController.view.hidden=true;
    [acController.view removeFromSuperview];
}

View My Profile

Comments

  1. Yami immigration is a well-known and experienced immigration consultant in Surat. We provide Immigration Services for many countries such as Italy, Canada, France, Australia, Germany, USA, Malaysia, New Zealand, and Singapore.

    ReplyDelete

Post a Comment

Most Visited

iOS Developer Sample Resume

Progress Indicators (Loading...)

Software Test Engineer Sample Resume