2017-12-18 244 views
1

请考虑谷歌地图gecoding响应的JSON响应。 它由具有五个元素的address_components组成。 通常情况下开发人员会使用它的第一个项目(索引为0的项目)来查找地址元素,例如ac routelocalityformatted address。但这不是最好的选择。例如,在这种情况下,第二个项目比其他项目更具描述性地址。有时在同一回复中有不同地点的同一地址。Google Map API:如何在地理编码结果中管理不同的“address_components”?

我该如何选择更好的一个(例如Android中的Google Map API在可用项目中选择一个,但它并不总是索引为0的项目,但它在替代项中进行了选择,我想知道它将如何执行并选择最好的搭配)?

再说,我也想通过不同address_components的所有元素进行迭代来提取和汇集细条信息,但问题是导致地址不一致的同类型一样locality元素在不同address_components不同的信息当从不同的address_component项目收集到的信息元素。 作为示例,在下面的示例中,我们有两个值شهر جدید اندیشهKaraj在不同的locality元素中,这两个元素是两个不同的城市标签(在同一邻域中)。

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

回答

1

它看起来像有一两个不同的问题,所以范围有点广。我会尽力解决你的疑惑。

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

enter image description here

正如你所看到的第一个项目都有一个类型路线,第二个项目都有一个类型sublocality,第三个有一个模式标本产地。

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

但无论如何,未命名道路是数据的问题,您可以按照帮助中心报告给谷歌的数据团队:

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

其次,如果我理解正确的话,你注意到了0项和1项尽管它们(我截图中的标记1和标记2)似乎属于安迪舍新城,但它们具有不同的局部地址组件值。路线(地点ID ChIJmf7_zEyTjT8RkM8-nK6dTm0)被报告为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新城的边界作为显示在下面的屏幕截图

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。

随意使用这个页面发送反馈给谷歌:

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

如果你想和反向地理编码玩,请使用地理编码工具:

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

希望我的答案澄清你的疑惑!

相关问题