解决此问题的一种方法是将两个地址转换为相同的格式。使用 Google Map Geocoding API 的一种简单方法是简单地将两个地址传递给 API 并获取输出。 Geocoding API 的输出类似于:
FORMAT OF GOOGLE'S GEODIRECTORY API (for reference):
{'results': [{'address_components': [{'long_name': '22',
'short_name': '22',
'types': ['street_number']},
{'long_name': 'Rue de Berri',
'short_name': 'Rue de Berri',
'types': ['route']},
{'long_name': 'Paris',
'short_name': 'Paris',
'types': ['locality', 'political']},
{'long_name': 'Département de Paris',
'short_name': 'Département de Paris',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'Île-de-France',
'short_name': 'IDF',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'France',
'short_name': 'FR',
'types': ['country', 'political']},
{'long_name': '75008', 'short_name': '75008', 'types': ['postal_code']}],
'formatted_address': '22 Rue de Berri, 75008 Paris, France',
'geometry': {'location': {'lat': 48.8728822, 'lng': 2.3054154},
'location_type': 'ROOFTOP',
'viewport': {'northeast': {'lat': 48.8743208802915,
'lng': 2.306719730291501},
'southwest': {'lat': 48.8716229197085, 'lng': 2.304021769708497}}},
'place_id': 'ChIJWxDbRsFv5kcRRcfu62JSRog',
'plus_code': {'compound_code': 'V8F4+55 Paris, France',
'global_code': '8FW4V8F4+55'},
'types': ['establishment', 'lodging', 'point_of_interest']}],
'status': 'OK'}
请注意 google 如何为您提供地址的不同组成部分,例如街道号码、位置等。现在您可以在这些组成部分之间进行加权/模糊匹配。由您决定是否要全部匹配,或者某些规则(例如街道号码或数字)应该始终匹配,如果 5 个匹配项中有 4 个匹配,则可以。您也可以考虑坐标之间的距离(注意:使用 Haversine 函数,而不仅仅是欧几里得参考:https://towardsdatascience.com/calculating-distance-between-two-geolocations-in-python-26ad3afe287b)。然后,您可以获得一个大于阈值的加权分数,以便他们被视为同一个地方。