使用pincode计算距离(Google map Distance Matrix API)

时间:2018-03-01 11:35:21

标签: google-maps google-maps-api-3

我正在开发MVC上的Web应用程序,我需要使用pin代码自动查找两个位置之间的距离。所以,我开始使用谷歌地图距离矩阵API,如下所示:

https://maps.googleapis.com/maps/api/distancematrix/xml?origins=" + Convert.ToString(origin) + "&destinations=" + Convert.ToString(destination) + "&key='MY key'

API适用于印度的密码,但我必须在柬埔寨部署应用程序,而柬埔寨的PIN代码则无法使用API​​。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

我查看了您的请求https://maps.googleapis.com/maps/api/distancematrix/xml?origins=12413&destinations=12211&key=YOUR_API_KEY

请注意,原点和目的地参数中的1241312211值非常不明确。您必须提供更精确的地址才能获得结果。距离矩阵API将在内部对1241312211进行地理编码。

例如,12413将被编码为“Cairo,NY 12413,USA”,这是美国的邮政编码。您可以在Geocoder工具中看到这一点:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D12413

12211将被地理编码为'Albany,NY 12211,USA':

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#q%3D12211

在这两种情况下,它都不是您所期望的柬埔寨的密码。

更重要的是,当我尝试使用组件过滤在柬埔寨找到邮政编码1241312211时,我得到了ZERO_RESULTS:

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#in_postal_code%3D12413%26in_country%3DKH%26options%3Dtrue

https://google-developers.appspot.com/maps/documentation/utils/geocoder/#in_postal_code%3D12211%26in_country%3DKH%26options%3Dtrue

尝试从柬埔寨的坐标反向地理编码中获取邮政编码也会导致ZERO_RESULTS:

https://maps.googleapis.com/maps/api/geocode/json?latlng=11.55486%2C104.905679&result_type=postal_code&key=MY_API_KEY

虽然可以找到相同坐标的位置:

https://maps.googleapis.com/maps/api/geocode/json?latlng=11.55486%2C104.905679&result_type=locality&key=MY_API_KEY

这是'Phnom Penh',地点ID为ChIJ42tqxz1RCTERuyW1WugOAZw

考虑到所有这些事实,Google在其数据库中没有针对柬埔寨的密码信息。

我建议您在应用程序中使用地点自动填充功能。这样,用户可以选择建议的地址,并且可以使用用户选择的地点ID来执行距离矩阵请求。还要了解官方文档中的地理编码最佳实践:

https://developers.google.com/maps/documentation/geocoding/best-practices

我希望这有帮助!

相关问题