Lasso Soft Inc. > Home

[google_geocode]

Linkgoogle_geocode
AuthorJason Huck
CategoryUtility
Version8.x
Licensehttp://opensource.org/licenses/artistic-license.php
Posted10 Jul 2006
Updated10 Jul 2006
More by this author...

Description

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].

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.

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

18 Nov 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;

15 Aug 2008, Dominique Guardiola Falco

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
))
);

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft