1

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" ],
    ...
}
MrUpsidown
  • 21,592
  • 15
  • 77
  • 131
Asterius
  • 2,180
  • 2
  • 19
  • 27
  • Did you try anything else by any chance? – MrUpsidown Apr 13 '16 at 14:16
  • I think postal codes are not considered as an address by the API. – MrUpsidown Apr 13 '16 at 15:25
  • Oh and by the way, 2566 seems to be an invalid postal code in Belgium. You got my downvote. – MrUpsidown Apr 13 '16 at 15:30
  • Well, you right, it seems this postal code does not exists. But anyway, my question remains valid and does not deserve a downvote: is there a way to force the country? In other words, I expect an answer where the destination does not exists instead of having somewhere in France or in the Philippines – Asterius Apr 13 '16 at 19:15
  • To react to your previous comments, yes postal codes are considered by the API. And yes, it works with some other postal code, but I can't test them all. Since my users select first their country, I would like to be sure to provide them a correct answer. – Asterius Apr 13 '16 at 19:18
  • Well I don't follow you here. What if you try with a **valid** postal code? https://maps.googleapis.com/maps/api/distancematrix/json?origins=Belgium+1200&destinations=Belgium+2500 No there is no way to biaise the country with the distanceMatrix. There are other ways you could do that (with the geocoder for example or with other web services). Why do you need the distanceMatrix? How many postal codes do you need to evaluate? – MrUpsidown Apr 14 '16 at 07:46
  • It seems to work with valid postal codes. My context: I want to evaluate the co2 footprint for a user moving from a place to another. In my application, I ask users to provide their country and their postal code, compute the distance between the two places (with distancematrix) and compute the footprint based on that. But I cannot ensure the information provided by the user are valid. When they are valid, the code seems to work as expected. But I cannot be sure of this, since I do not know how distancematrix works internally. And in the other case, it do not know how I can detect the error. – Asterius Apr 14 '16 at 08:58

1 Answers1

1

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.

MrUpsidown
  • 21,592
  • 15
  • 77
  • 131