Like most of the programmers that have to deal with languages other than english, I wish we were building english-only web applications, and support only english strings. I live in Quebec and almost all my projects have to deal with the french accents. It obviously brings a lot of challenges when dealing with external systems not using the UTF-8 encoding (like Hotmail for instance which is still on ISO-8859-1, or copy & paste from a French Word document). But I will keep this discussion for another post.
In one of my current projects, I need to integrate GoogleMap in order to get the geocode of a physical address. Piece of cake you would say: just use the google-geocode gem. Well, I tried. It worked really great until I tried searching for a Quebec address with french accents. It failed!
I then told myself that it wasn’t that complicated to implement it myself. I just query Google using a URL like http://maps.google.com/maps/geo?q=addressToLookFor&output=json&key=yourGoogleKey and voila, you get JSON results and simply have to parse it. Well, it is not that straight forward. Here’s what I found:
- By default, Google replies using the ISO-8859-1 encoding. The JSON parser doesn’t like that. To force UTF-8, you have to pass an extra parameter in the URL: oe=utf8. Where did I get this? From the bug list of the API.
- The results that Google sends back are not the same as the ones you get from a manual search on maps.google.ca. Why? I don’t know. I guess the web site does additional massaging to the results.
- Results may contain some strange things. For example, it you search for “455 st-pierre, montreal, qc, canada”, you get more than one address and one of them is in the Matawinie sub-administrative area, in the Montreal locality, which doesn’t make any sense. So which one is the right one? Well, the machine can’t figure it out, we have to ask the human in front of the computer to pick the right one.
- You sometimes get results that do not start with a numeric address (it looks like a generic geocode for the street only). I simply convert the address to an integer (bla.to_i), and if it returns something higher than 0, than I consider this address as a potential good one.
So I hope this saves you some time if you ever have to do some geocoding mapping in your application.
Thanks for listening! 😉
P.S. I just found out that 3 days ago a new version of GeoX got out. I will give this plugin a try to see if it works as I would like it to work. I will post an update soon.