1 package Geo::TomTom::Geocoding;
8 use base 'HTTP::Response';
9 use JSON 'decode_json';
13 Geo::TomTom::Geocoding - Interface to the TomTom Geocoding service
21 our $VERSION = '0.01';
26 use Geo::TomTom::Geocoding;
29 key => '12345678abcdef',# API authorization
30 ST => '123', # address number
31 T => 'Main Street', # street name
32 L => 'San Francisco', # locality
33 AA => 'CA', # administrative area (state/province/district)
34 PC => '93102', # postal code
35 CC => 'USA', # country code (ISO three-letter)
36 IS => 'Market Street', # intersecting street
37 # other options specified in the API docs
39 $result = Geo::TomTom::Geocoding->query($request);
41 if ($result->is_success) {
42 foreach ($result->locations) {
43 print $_->{formattedAddress};
51 Send a request to the web service. See
52 L<http://developer.tomtom.com/docs/read/geocoding/Request> for fields
53 that can be included in HASHREF. C<key> is your API key.
55 Returns an object of class Geo::TomTom::Geocoding.
59 my $ua = LWP::UserAgent->new;
60 my $base_uri = 'https://api.tomtom.com/lbs/geocoding/geocode';
65 if (ref($_[0]) eq 'HASH') {
66 $opt = [ %{ $_[0] } ];
67 } elsif (ref($_[0]) eq 'ARRAY') {
72 push @$opt, format => 'json';
73 my $uri = URI->new($base_uri);
74 $uri->query_form($opt);
75 warn "Geo::TomTom::Geocoding->request\n$uri\n\n" if $DEBUG;
76 my $http_req = HTTP::Request->new(GET => $uri->as_string);
77 my $self = $ua->request($http_req);
79 if ( $self->is_success ) {
80 my $data = decode_json($self->content);
81 if ( $data->{geoResponse}->{count} == 0 ) {
82 $self->message("Location not found");
84 $self->locations( @{$data->{geoResponse}->{geoResult}} );
95 Returns a list of all locations matched by the address query. Each one
96 will be a hashref containing any of the elements listed in the documentation:
97 L<http://developer.tomtom.com/docs/read/geocoding/Response>
104 if ( ref($_[0]) eq 'ARRAY' ) {
105 $self->{_locations} = shift;
106 } elsif ( ref($_[0]) eq 'HASH' ) {
107 $self->{_locations} = [ @_ ];
110 @{ $self->{_locations} || [] };
115 Mark Wells, C<< <mark at freeside.biz> >>
119 You can find documentation for this module with the perldoc command.
121 perldoc Geo::TomTom::Geocoding
123 For the request and response attributes, see the TomTom API documentation:
125 L<developer.tomtom.com/docs/read/geocoding>
127 Commercial support for this module is available from Freeside Internet
130 L<http://www.freeside.biz/>
135 =head1 LICENSE AND COPYRIGHT
137 Copyright (C) 2013 Freeside Internet Services, Inc.
139 This program is free software; you can redistribute it and/or modify it
140 under the terms of either: the GNU General Public License as published
141 by the Free Software Foundation; or the Artistic License.
143 See http://dev.perl.org/licenses/ for more information.
145 The data from the TomTom geocoding service is NOT free software. Consult
146 TomTom's terms of service for details.
150 1; # End of Geo::TomTom::Geocoding