Google Map API:如何在地理编码结果中管理不同的“address_components”?

问题描述 投票:1回答:1

考虑以下谷歌地图gecoding响应的JSON响应。它由一系列带有五个元素的address_components组成。通常开发人员使用其第一个项目(索引为0的项目)来查找地址元素,例如ac routelocality以及formatted address。但这不是最好的选择。例如,在这种情况下,第二项是比其他项更具描述性的地址。有时在同一响应中有相同的地址和不同的地点。

我如何选择更好的一个(例如,Android中的Google Map API选择一个可用项目,但它并不总是带有索引0的项目,但它在备选项中选择,我想知道它将如何做并选择最佳匹配) ?

此外,我想知道迭代不同address_components的所有元素来提取和汇集更精细的信息,但问题是相同类型的元素的信息,如locality在不同的address_components有不同的信息,导致信息元素时的地址不一致从不同的address_component项目收集。例如,在下面的示例中,我们在不同的شهر جدید اندیشه元素中有两个值Karajlocality,它们是两个不同的城市标签(在同一邻域中)。

https://maps.googleapis.com/maps/api/geocode/json?latlng=35.7163931455472,51.01335000246763&key=xxxxx&language=fa

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Unnamed Road",
               "short_name" : "Unnamed Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Karaj",
               "short_name" : "Karaj",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Shahriar County",
               "short_name" : "Shahriar County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tehran Province",
               "short_name" : "Tehran Province",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tehran Province, Karaj, Unnamed Road, ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.7221382,
                  "lng" : 51.0148178
               },
               "southwest" : {
                  "lat" : 35.716435,
                  "lng" : 51.0095103
               }
            },
            "location" : {
               "lat" : 35.719286,
               "lng" : 51.012165
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.7221382,
                  "lng" : 51.0148178
               },
               "southwest" : {
                  "lat" : 35.716435,
                  "lng" : 51.0095103
               }
            }
         },
         "place_id" : "ChIJmf7_zEyTjT8RkM8-nK6dTm0",
         "types" : [ "route" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "فاز ۶ شهر جدید اندیشه",
               "short_name" : "فاز ۶ شهر جدید اندیشه",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "شهر جدید اندیشه",
               "short_name" : "شهر جدید اندیشه",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "شهرستان شهریار",
               "short_name" : "شهرستان شهریار",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "استان تهران",
               "short_name" : "استان تهران",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "فاز ۶ شهر جدید اندیشه، شهر جدید اندیشه، استان تهران، ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.7389537,
                  "lng" : 51.0349971
               },
               "southwest" : {
                  "lat" : 35.7039031,
                  "lng" : 51.0044146
               }
            },
            "location" : {
               "lat" : 35.7210753,
               "lng" : 51.014934
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.7389537,
                  "lng" : 51.0349971
               },
               "southwest" : {
                  "lat" : 35.7039031,
                  "lng" : 51.0044146
               }
            }
         },
         "place_id" : "ChIJiYlwJLHsjT8RruE39U9NMoQ",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "شهر جدید اندیشه",
               "short_name" : "شهر جدید اندیشه",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "شهرستان شهریار",
               "short_name" : "شهرستان شهریار",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "استان تهران",
               "short_name" : "استان تهران",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "شهر جدید اندیشه، استان تهران، ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.7388144,
                  "lng" : 51.04460479999999
               },
               "southwest" : {
                  "lat" : 35.6838973,
                  "lng" : 50.9894371
               }
            },
            "location" : {
               "lat" : 35.7078282,
               "lng" : 51.0227587
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.7388144,
                  "lng" : 51.04460479999999
               },
               "southwest" : {
                  "lat" : 35.6838973,
                  "lng" : 50.9894371
               }
            }
         },
         "place_id" : "ChIJIQwwRcnsjT8RnTJfLJ3QUAg",
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "شهرستان شهریار",
               "short_name" : "شهرستان شهریار",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "استان تهران",
               "short_name" : "استان تهران",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "شهرستان شهریار، استان تهران، ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.7389885,
                  "lng" : 51.23044970000001
               },
               "southwest" : {
                  "lat" : 35.5333437,
                  "lng" : 50.8859253
               }
            },
            "location" : {
               "lat" : 35.6096201,
               "lng" : 51.03319330000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.7389885,
                  "lng" : 51.23044970000001
               },
               "southwest" : {
                  "lat" : 35.5333437,
                  "lng" : 50.8859253
               }
            }
         },
         "place_id" : "ChIJQ6KQjY7xjT8RoYD9gJh8_CY",
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "استان تهران",
               "short_name" : "استان تهران",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "استان تهران، ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.07789210000001,
                  "lng" : 53.216228
               },
               "southwest" : {
                  "lat" : 34.909543,
                  "lng" : 50.3186971
               }
            },
            "location" : {
               "lat" : 35.7248416,
               "lng" : 51.381653
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.07789210000001,
                  "lng" : 53.216228
               },
               "southwest" : {
                  "lat" : 34.909543,
                  "lng" : 50.3186971
               }
            }
         },
         "place_id" : "ChIJf5Us9YQBjj8R0OohvHQms1U",
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "ایران",
               "short_name" : "IR",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "ایران",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.782056,
                  "lng" : 63.3333366
               },
               "southwest" : {
                  "lat" : 24.8066999,
                  "lng" : 44.0326949
               }
            },
            "location" : {
               "lat" : 32.427908,
               "lng" : 53.688046
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.782056,
                  "lng" : 63.3333366
               },
               "southwest" : {
                  "lat" : 24.8066999,
                  "lng" : 44.0326949
               }
            }
         },
         "place_id" : "ChIJ8R1rwS7s9z4RzvpRntOVsEA",
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}
android google-maps google-geocoding-api
1个回答
3
投票

看起来有两个不同的问题,因此范围有点宽泛。我会尽力解决你的疑虑。

首先,您使用反向地理编码请求将坐标35.7163931455472,51.01335000246763解析为街道地址。请注意,reverse geocoding旨在将给定坐标解析为最近的可用地址。除街道地址外,该服务还将建议不同类型的结果:路线,地方性,地点,行政区域,国家。有时,如果在给定点附近没有街道地址(大约距离该点50米以内),则响应中不会出现类型街道地址的结果。在您的示例中,类型街道地址的结果不可用,因此响应包含从路由级别开始的不同类型的结果。看看我的截图,它代表了您的回复中的前三项。

enter image description here

如您所见,第一个项目具有类型路由,第二个项目具有类型子网站,第三个项目具有类型位置。

很难说选择地址的“最佳”标准是什么。您可以看到路线项目是最接近的路线项目,但不幸的是这条道路未命名,因此您认为它不够好。从技术上讲,这是最接近的项目,因此服务假设这是最佳匹配。我建议检查结果的类型,如果项目0有类型street_address应该足够好去,如果项目0有类型路线,检查道路是否有名称,如果这是未命名道路检查项目1可能更详细。

无论如何,未命名的道路是数据问题,您可以在帮助中心之后向Google数据团队报告:

https://support.google.com/maps/answer/3094088

其次,如果我理解正确,你注意到项目0和项目1具有不同的地点地址组件值,尽管它们(我的屏幕截图中的标记1和标记2)似乎属于Andisheh新城。路线(地点ID ChIJmf7_zEyTjT8RkM8-nK6dTm0)被报告为Karaj地区的一部分。我可以在Google地图上看到Karaj的界限,如下面的屏幕截图所示

enter image description here

https://www.google.com/maps/place/Karaj,+Alborz+Province,+Iran/@35.7700272,50.95899,12.41z/data=!4m5!3m4!1s0x3f8dbf95ef45f011:0x722a04e54eba9bcd!8m2!3d35.8400188!4d50.9390906

我可以看到Andisheh New Town的界限,如下面的截图所示

enter image description here

https://www.google.com/maps/place/Andisheh+New+Town,+Tehran+Province,+Iran/@35.6998795,51.0216598,14.08z/data=!4m5!3m4!1s0x3f8decc945300c21:0x850d09d2c5f329d!8m2!3d35.7078282!4d51.0227587

请注意,您的回复路线显然位于Andisheh New Town多边形内部,因此在这一点上看起来我们也面临着数据问题。地点ID ChIJmf7_zEyTjT8RkM8-nK6dTm0应该有Andisheh New Town的地方组成部分,而不是Karaj。

您可以使用此页面向Google发送反馈:

https://www.google.com/maps/place/Tehran+Province,+Karaj,+Unnamed+Road,+Iran/@35.719286,51.0077876,17z/data=!3m1!4b1!4m5!3m4!1s0x3f8d934cccfffe99:0x6d4e9dae9c3ecf90!8m2!3d35.719286!4d51.012165!10m2!1e3!2e3

如果您想使用反向地理编码,请使用Geocoder工具:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D35.716393%252C51.01335

我希望我的回答能澄清你的疑惑!

© www.soinside.com 2019 - 2024. All rights reserved.