简体   繁体   中英

Get distancematrix by postal code

I am using the distancematrix of Google Maps API to get the distance between two postal codes in Belgium. I face the issue that the country is evaluated in the whole address. Is there a way to force the country in the request?

For example:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Belgium+1200&destinations=Belgium+2566

Provides a destination in the Philippines instead of Belgium:

{
    "destination_addresses" : [ "Belgium, Dasmariñas, Cavite, Philippines" ],
    "origin_addresses" : [ "1200, Belgique" ],
    ...
}

Same issue if I use the french name of the country (Belgique) in the request:

https://maps.googleapis.com/maps/api/distancematrix/json?origins=Belgium+1200&destinations=Belgium+2566

Provides a destination in France:

{
    "destination_addresses" : [ "2566 Rue de Belgique, 66140 Canet-en-Roussillon, France" ],
    "origin_addresses" : [ "1200, Belgique" ],
    ...
}

After getting some more information, I think I can come up with an answer.

Is it only for Belgium? Could you not let them select only valid postal codes? That would avoid many errors... But if you can't, then you can try other options.


Now for the answer:

You mentioned that you want to compute the distance between the two places (with distancematrix) . You don't need to use the distanceMatrix if you need to compute distances between only 2 places. You can use the standard Directions API (or service as part of the Google Maps Javascript API v3). In any case, with both Directions and distanceMatrix, you have to display the information returned by the service on a map (this is in their terms of service).

The advantage of the Directions API is that you can biaise your request to a particular region (country) using the IANA language region subtags and the region parameter.

var start = "1200, Belgium";
var end = "2500, Belgium";
var method = 'DRIVING';
var request = {
    origin: start,
    destination: end,
    region: 'BE',
    travelMode: google.maps.DirectionsTravelMode[method]
};

The documentation is here: https://developers.google.com/maps/documentation/javascript/directions#Directions

There are also other services that you could use, prior to retrieving the directions to make sure that the origin and destination values are correct.

The Geocoder could be one (you can also biaise your requests with region codes).

Geonames.org also has a dedicated postal code search webservice. It takes an optional (multiple) country code as parameter. It will return coordinates that you could then use in your directions request.

You can check the below fiddle for a complete example using the standard Directions service. Play with it and see if it works for you. Try to enter invalid postal codes, or even with a valid postal code but remove "Belgium" from the origin or destination strings and you will see what happens...

JSFiddle demo

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM