首页 文章

Google Map 会解决自动填充和地理编码API返回差异地址

提问于
浏览
0

我正在使用谷歌 Map 自动填充API自动完成地址由用户键入,一旦我完成所选地址,从谷歌地方自动填充地址传递选定的地址字符串谷歌 Map geocode api,获取详细的组件,问题有时是地理编码API即使autocomplete API返回相同的地址,也不返回街道号码,不知道它出错的地方,下面是我正在使用的代码和两个服务的输出

这就是我从Geocode服务中检索详细地址的方法

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;
        }

以下是地址的示例,其中地址具有街道号,但地理编码点返回该地址

{
         "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" ]
      }

以下是针对上述自动填充地址的Google Map 地理编码API的输出

{
    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;
}

输出doest具有组件类型“Street number”,其中自动完成的地址字符串在开头就是正确的 . 许多地址都会发生这种情况,可能出现什么问题?还是我需要使用其他服务?具有街道号码的地址的示例如下

这是由自动完成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" ]
      }

以下是在将自动完成地址作为输入发送后由Geocode服务返回的结果

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;
}

任何帮助将受到高度赞赏 .

1 回答

  • 2

    你在自动完成结果的描述中得到的只是一个字符串,仅此而已(不是一组功能,如街道,邮编或房子编号) . 服务 tries 找到确切的位置(基于输入),但匹配可能是部分的 .

    当你例如在http://maps.google.com输入 740 Spring Lane, Wallacedale, Victoria, Australia 你会得到 Spring Ln, Wallacedale VIC 3303,Australia 的结果

    您也可以输入 12345 Spring Lane, Wallacedale, Victoria, Australia ,您将获得相同的结果 .

    除此之外:我建议request the place-details基于 place_id . 虽然它不能给你想要的结果(房子数),结果应该更精确(绑定到一个特定的地方而不是一个地方的地址) . 它还将返回该地点的更多详细信息 .

相关问题