Retrieving a processed and correctly formatted address from Google Maps

I am trying to use the Google Maps API services in my iOS app to convert a string containing an address to a properly parsed and legitimate address. I understand that the Google API does not validate the address; I just need it to parse the string into a full logical address.

After doing some searching on the API website, the only way I have found this is to use the geocoding feature to convert the input string to coordinates and then use reverse geocoding to convert it to something that I can parse, Is there a way to do is it at a lower cost? Doing it this way seems to be a bit backward.

Example: 347 N Canon Dr Beverly Hills, CA 90210. Google Maps is the best choice for intuitively separating different parts of an address. Otherwise, I run the risk of dealing with issues like 1040 8th Street becoming 104th Street or something. I want to be able to enter an address that is mostly formatted correctly and for Google to use the API to fix it and return what I can pass to the web service.

+3


source to share


1 answer


I think you want NSDataDetector

one that is built right into Cocoa Touch.

It has a subtype NSTextCheckingTypeAddress

that will try to parse addresses for you.

Using one in the example line:

NSString * address = @"347 N Canon Dr Beverly Hills, CA 90210";
NSError * err;
NSDataDetector * addrParser = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeAddress
                                                              error:&err];
__block NSDictionary * addressParts;
if( addrParser ){
    [addrParser enumerateMatchesInString:address
                                 options:0
                                   range:(NSRange){0, [address length]}
                              usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
                                  addressParts = [result addressComponents];
    }];
}

NSLog(@"%@", addressParts);

      



Produces this dictionary:

2014-09-25 20:26:57.747 ParseAddress[56154:60b] {
    City = "Beverly Hills";
    State = CA;
    Street = "347 N Canon Dr";
    ZIP = 90210;
}

      

possible keys for which are listed in the docs .

+5


source







All Articles