1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
define_tag(
'geocode',
-namespace='google_',
-req='key',
-req='address', -copy,
-priority='replace',
-description='Geocodes the given address via Google.'
);
local('out' = map);
local('codes') = map(
200 = 'G_GEO_SUCCESS',
601 = 'G_GEO_MISSING_ADDRESS',
602 = 'G_GEO_UNKNOWN_ADDRESS',
603 = 'G_GEO_UNAVAILABLE_ADDRESS',
610 = 'G_GEO_BAD_KEY',
620 = 'G_GEO_TOO_MANY_QUERIES',
500 = 'G_GEO_SERVER_ERROR'
);
local('getparams') = array(
'q' = #address,
'output' = 'xml',
'key' = #key
);
protect;
local('data') = xml_tree(
include_url(
'http://maps.google.com/maps/geo',
-getparams=#getparams
)
);
local('code') = integer(#data->response->status->code->contents);
if(#code == 200);
local('coords') = #data->response->placemark->point->coordinates->contents;
#out->insert('longitude' = decimal(@#coords->split(',')->first));
#out->insert('latitude' = decimal(@#coords->split(',')->second));
return(#out);
else;
return(#codes->find(#code));
/if;
handle_error;
return('Unknown error.');
/handle_error;
/protect;
/define_tag;
|
Accented letters fix
When geocoding in countries using accented letters, the output from Google throws a libxml2 error when passed to xml_tree. As gary Clark pointed it out on Lassotalk, the fix is : protect; local('data') = xml_tree(encode_smart( include_url( 'http://maps.google.com/maps/geo', -getparams=#getparams )) );