2

I am using google maps places autocomplete API to autocomplete address typed by user and once i am done with selection of required address , passing selected address string from google places autocompleted address to google maps geocode api, to get detailed components, problem is sometimes geocode API doesn't return street numbers even when the same address returned by autocomplete API has it, not sure where its going wrong , below is the code i m using and the output from both the services

This is how I am retrieving detailed address from Geocode service

 NSString* address = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=false",completionText];
    NSString* str = [address stringByReplacingOccurrencesOfString:@","
                                                       withString:@""];
    str = [str stringByReplacingOccurrencesOfString:@" "
                                         withString:@"+"];


    NSURL *requestURL = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:(requestURL)];

    //response
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSError *jsonParsingError = nil;
    NSDictionary *locationResults = [NSJSONSerialization JSONObjectWithData:response options:0 error:&jsonParsingError];

    NSLog(@"%@",locationResults);
    NSDictionary* results = [[locationResults objectForKey:@"results"] objectAtIndex:0];
    NSArray* address_components = [results objectForKey:@"address_components"];


    for (id addr in address_components) {
        NSString *long_name = [addr valueForKey:@"long_name"];
        NSString *short_name = [addr valueForKey:@"short_name"];
        NSString *types = [[addr valueForKey:@"types"] objectAtIndex:0];


        if ([types rangeOfString:@"street_number"].location != NSNotFound)
        {
            streetNum = long_name;
        }
        else if ([types rangeOfString:@"route"].location != NSNotFound)
        {
            streetName = long_name;
        }
        else if ([types rangeOfString:@"locality"].location != NSNotFound)
        {
            suburb = long_name;
        }
        else if ([types rangeOfString:@"administrative_area_level_1"].location != NSNotFound)
        {
            state = short_name;
        }
        else if ([types rangeOfString:@"country"].location != NSNotFound)
        {
            country = short_name;
        }
        else if ([types rangeOfString:@"postal_code"].location != NSNotFound)
        {
            postalCode = short_name;
        }

Here is the example of an address where address has street number but geocode dots return it

 {
         "description" : "740 Spring Lane, Wallacedale, Victoria, Australia",
         "id" : "ad2c41d2f934a30f4c5f8c4b336df8de988174d6",
         "matched_substrings" : [
            {
               "length" : 10,
               "offset" : 0
            },
            {
               "length" : 8,
               "offset" : 30
            },
            {
               "length" : 9,
               "offset" : 40
            }
         ],
         "place_id" : "EjE3NDAgU3ByaW5nIExhbmUsIFdhbGxhY2VkYWxlLCBWaWN0b3JpYSwgQXVzdHJhbGlh",
         "reference" : "CkQ1AAAASPpKC9JU4XQoFf3LUYwoF_hJFRpFt5VE67NhCPs8vV9ghXvVUeKgPm-BH-Owx-zvuhHEPhJU-Y2ReK5S49AdphIQiKpvir0TegpxEAjmmNJEJhoUCt7KXyZ6LoN51qT50Unrl5ceYMU",
         "terms" : [
            {
               "offset" : 0,
               "value" : "740 Spring Lane"
            },
            {
               "offset" : 17,
               "value" : "Wallacedale"
            },
            {
               "offset" : 30,
               "value" : "Victoria"
            },
            {
               "offset" : 40,
               "value" : "Australia"
            }
         ],
         "types" : [ "route", "geocode" ]
      }

Following is the output from Google map geocode API for the above autocompleted address

{
    results =     (
                {
            "address_components" =             (
                                {
                    "long_name" = "Spring Lane";
                    "short_name" = "Spring Ln";
                    types =                     (
                        route
                    );
                },
                                {
                    "long_name" = Wallacedale;
                    "short_name" = Wallacedale;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = Victoria;
                    "short_name" = VIC;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Australia;
                    "short_name" = AU;
                    types =                     (
                        country,
                        political
                    );
                },
                                {
                    "long_name" = 3303;
                    "short_name" = 3303;
                    types =                     (
                        "postal_code"
                    );
                }
            );
            "formatted_address" = "Spring Lane, Wallacedale VIC 3303, Australia";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "-37.9132082";
                        lng = "141.8506572";
                    };
                    southwest =                     {
                        lat = "-37.9283224";
                        lng = "141.8505337";
                    };
                };
                location =                 {
                    lat = "-37.9266631";
                    lng = "141.8506572";
                };
                "location_type" = "GEOMETRIC_CENTER";
                viewport =                 {
                    northeast =                     {
                        lat = "-37.9132082";
                        lng = "141.8519444302915";
                    };
                    southwest =                     {
                        lat = "-37.9283224";
                        lng = "141.8492464697085";
                    };
                };
            };
            "partial_match" = 1;
            "place_id" = "ChIJV74lHNPCzGoRnS8t_EDR8zk";
            types =             (
                route
            );
        }
    );
    status = OK;
}

The output doest have component type "Street number" where as autocompleted address string has it right in the begining. This happens with many addresses , what can be wrong ? or do i need to use some other service ? Example of an address which has street number is below

This is returned by autocomplete API

{
         "description" : "11 Victoria Parade, Collingwood, Victoria, Australia",
         "id" : "dbeebdb81c8babd166ac8f89878e88c2cba333a1",
         "matched_substrings" : [
            {
               "length" : 18,
               "offset" : 0
            },
            {
               "length" : 9,
               "offset" : 43
            }
         ],
         "place_id" : "EjQxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kLCBWaWN0b3JpYSwgQXVzdHJhbGlh",
         "reference" : "CkQ4AAAATy4Z7xc3ermqeWnlxpl81pAVsBWOIl3S2JbCwwRxraLt39OJauWFnCR3NgGyruVBXfUo236mMe6CCQt5XRSYwRIQEL8JC7Qo8j3-0iD4AnJTtBoURjzgm8aHBa2g0tY4lkTKYTuVGPg",
         "terms" : [
            {
               "offset" : 0,
               "value" : "11 Victoria Parade"
            },
            {
               "offset" : 20,
               "value" : "Collingwood"
            },
            {
               "offset" : 33,
               "value" : "Victoria"
            },
            {
               "offset" : 43,
               "value" : "Australia"
            }
         ],
         "types" : [ "route", "geocode" ]
      }

And below is the result returned by Geocode service after sending the autocompleted address as an input

results =     (
                {
            "address_components" =             (
                                {
                    "long_name" = 11;
                    "short_name" = 11;
                    types =                     (
                        "street_number"
                    );
                },
                                {
                    "long_name" = "Victoria Parade";
                    "short_name" = "Victoria Parade";
                    types =                     (
                        route
                    );
                },
                                {
                    "long_name" = Collingwood;
                    "short_name" = Collingwood;
                    types =                     (
                        locality,
                        political
                    );
                },
                                {
                    "long_name" = Victoria;
                    "short_name" = VIC;
                    types =                     (
                        "administrative_area_level_1",
                        political
                    );
                },
                                {
                    "long_name" = Australia;
                    "short_name" = AU;
                    types =                     (
                        country,
                        political
                    );
                },
                                {
                    "long_name" = 3066;
                    "short_name" = 3066;
                    types =                     (
                        "postal_code"
                    );
                }
            );
            "formatted_address" = "11 Victoria Parade, Collingwood VIC 3066, Australia";
            geometry =             {
                bounds =                 {
                    northeast =                     {
                        lat = "-37.8087633";
                        lng = "144.9830508";
                    };
                    southwest =                     {
                        lat = "-37.8087778";
                        lng = "144.9830487";
                    };
                };
                location =                 {
                    lat = "-37.8087633";
                    lng = "144.9830508";
                };
                "location_type" = "RANGE_INTERPOLATED";
                viewport =                 {
                    northeast =                     {
                        lat = "-37.8074215697085";
                        lng = "144.9843987302915";
                    };
                    southwest =                     {
                        lat = "-37.8101195302915";
                        lng = "144.9817007697085";
                    };
                };
            };
            "place_id" = EjMxMSBWaWN0b3JpYSBQYXJhZGUsIENvbGxpbmd3b29kIFZJQyAzMDY2LCBBdXN0cmFsaWE;
            types =             (
                "street_address"
            );
        }
    );
    status = OK;
}

Any help will be highly appreciated.

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93

1 Answers1

2

What you get in the description by the autocomplete-result is just a string, nothing more(not a set of features like street,zip or housenumber). The service tries to find the exact place(based on the input), but the match may be partially.

When you e.g. type 740 Spring Lane, Wallacedale, Victoria, Australia at http://maps.google.com you'll get a result for Spring Ln, Wallacedale VIC 3303,Australia

You may also type 12345 Spring Lane, Wallacedale, Victoria, Australia , and you'll get the same result.

Apart from that: I would suggest to request the place-details based on the place_id . Although it must not give you the desired result(a housenumber), the result should be more exact(bound to a particular place instead of the address of a place). It will also return more details for the place.

Dr.Molle
  • 116,463
  • 16
  • 195
  • 201