Tuesday 5 August 2014

Get Address from Coordinate or CLLocation


CLLocation *loc = [[CLLocation alloc]initWithLatitude:37.3104 longitude:-122.0108];


[self getAddressFromLocation:loc];



-(void)getAddressFromLocation:(CLLocation*)loc
{
    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    
    
    [ceo reverseGeocodeLocation:loc completionHandler:^(NSArray *placemarks, NSError *error)
     {
         
         NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
         
         
         
         NSLog(@"placemarks=%@",[placemarks objectAtIndex:0]);
         CLPlacemark *placemark1 = [placemarks objectAtIndex:0];
         
         NSLog(@"placemark.ISOcountryCode =%@",placemark1.ISOcountryCode);
         NSLog(@"placemark.country =%@",placemark1.country);
         NSLog(@"placemark.postalCode =%@",placemark1.postalCode);
         NSLog(@"placemark.administrativeArea =%@",placemark1.administrativeArea);
         NSLog(@"placemark.locality =%@",placemark1.locality);
         NSLog(@"placemark.subLocality =%@",placemark1.subLocality);
         NSLog(@"placemark.subThoroughfare =%@",placemark1.subThoroughfare);
         
         
         NSString *locatedaddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         
         //Print the location in the console
         NSLog(@"Currently address is: %@",locatedaddress);
         
         
         if (error == nil && [placemarks count] > 0)
         {
             placemark = [placemarks lastObject];
             
             // strAdd -> take bydefault value nil
             NSString *strAdd = nil;
             
             if ([placemark.subThoroughfare length] != 0)
                 strAdd = placemark.subThoroughfare;
             
             if ([placemark.thoroughfare length] != 0)
             {
                 // strAdd -> store value of current location
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]];
                 else
                 {
                     // strAdd -> store only this value,which is not null
                     strAdd = placemark.thoroughfare;
                 }
             }
             
             if ([placemark.postalCode length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]];
                 else
                     strAdd = placemark.postalCode;
             }
             
             if ([placemark.locality length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]];
                 else
                     strAdd = placemark.locality;
             }
             
             if ([placemark.administrativeArea length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]];
                 else
                     strAdd = placemark.administrativeArea;
             }
             
             if ([placemark.country length] != 0)
             {
                 if ([strAdd length] != 0)
                     strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]];
                 else
                     strAdd = placemark.country;
             }
             
             
             NSLog(@"Address : %@", strAdd);
             
             
             
             address = strAdd;
             
         }
         
         
     }];
    
}

No comments:

Post a Comment