[google_geocode]

Description

Link: [google_geocode]
Author: Jason Huck
Category: Utility
Version: 8.x
License:
Posted: Jul. 10, 2006
Updated: Jan. 01, 0001
More by this author...
This tag returns a map of longitude and latitude for the given street address using the Google Maps API. Accepts two parameters: a valid API key and a street address. Requires [xml_tree].

Parameters

-key string, required A valid Google Maps API key.
-address string, required The street address to geocode.

Sample Usage

google_geocode(
    -key='your_api_key_here',
    -address'your street address here'
);
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
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;

 

Related Tags



Comments

11/18/2009, Paul Fabris
If "placemark" is an array, you get an error
I was getting "unknown error" with a particular address. Turns out Google was returning 2 placemark elements instead of a single one, which would cause the code to barf because it was expecting a string, not an array of elements Fix: Favour the first element of the returned placemark array. Line 38: Replace with the following: local('placemark')=#data->response->placemark; if(#placemark->isa('array')); local('coords') = #placemark->get(1)->point->coordinates->contents; else; local('coords') = #placemark->point->coordinates->contents; /if;
08/15/2008, Dominique Guardiola
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 )) );
Email:


Password:



Newest

Most Popular

Support tagSwap.net