{"id":290,"date":"2013-04-28T22:25:28","date_gmt":"2013-04-29T03:25:28","guid":{"rendered":"http:\/\/www.aibistin.com\/?p=290"},"modified":"2013-08-25T21:10:51","modified_gmt":"2013-08-26T02:10:51","slug":"travel-time","status":"publish","type":"post","link":"https:\/\/www.aibistin.com\/?p=290","title":{"rendered":"Perl Google Distance Matrix API Interface"},"content":{"rendered":"<p class=\"important\">\nThis is my Perl Module <a class=\"vital-link\" href=\"https:\/\/github.com\/aibistin\/Google-Travel-Matrix\" title=\"Google::Travel::Matrix\"  target=\"_blank\">Google::Travel::Matrix<\/a>, which will be an interface to the Google Distance Matrix API. I will use this for calculating distance and travel times between multiple locations.<\/p>\n<p><p class=\"code-heading\">Google Distance Matrix API Interface<\/p>\n<p><span class=\"code-description\">Google::Travel::Matrix<\/span><\/p>\n<p>\nThis module provides an interface with the Google Distance Matrix API. It will return the distance between given locations in JSON format.<br \/>\nIt will accept one or many &#8220;Origin&#8221; addresses in String, Array or Hash format.It will also accept many Destination addresses. The Google Matrix configuration attributes can be set here, but it may be easier just to accept the defaults that I built in unless you have other specific requirements.<br \/>\nI used the Perl Moose Object framework as it does a really great job of helping set up the configuration attributes using Moose Types and coercion. It will also make it easier for me to build my sub classes.<br \/>\nThe module is available here on GitHub,<tad-larger> <a class=\"vital-link\" href=\"https:\/\/github.com\/aibistin\/Google-Travel-Matrix\" title=\"Google::Travel::Matrix\"  target=\"_blank\">Google::Travel::Matrix<\/a><br \/>\n<\/tad-larger>\n<\/p>\n<pre class=\"brush: css; auto-links: false; collapse: false; first-line: 1; gutter: true; light: false; pad-line-numbers: false; smart-tabs: false; tab-size: 4; title: Sample code from Google::Travel::Matrix; toolbar: true; notranslate\" title=\"Sample code from Google::Travel::Matrix\">\r\n\r\n=head2 get_all_elements\r\n Given the Google Distance Matrix output as a scalar reference to a Perl data\r\n structure, returns an ArrayRef of Matrix elements or undef.\r\n If no Google Output data passed, then it will create one using the Origin and\r\n destination addresses;\r\n It would be a good idea to check that the Google Matrix Return is 'OK' before calling\r\n this method.\r\n=cut\r\n\r\nsub get_all_elements {\r\n    my $self = shift;\r\n    my $matrix = shift || $self-&gt;get_google_matrix_data_as_scalar_ref();\r\n\r\n    my $google_status = $self-&gt;get_matrix_status_message($matrix);\r\n\r\n    if ( ( not $google_status ) || ( $google_status ne $VALID_REQ ) ) {\r\n\r\n        $log-&gt;debug( 'Google return status message is: '\r\n              . ( $google_status \/\/ 'No Google Status' ) );\r\n        return $FAIL;\r\n    }\r\n\r\n    #------ Preserve the Original address sent to Google\r\n    #       as it may be needed later.\r\n    #       Yet again,  this is something I would prefer \r\n    #       to put into a Child Class\r\n    my $original_origins      = $self-&gt;_get_array_of_origins();\r\n    my $original_destinations = $self-&gt;_get_array_of_destinations();\r\n\r\n    #------ Get each combination for the origin destination addresses\r\n    my $origin_ct = 0;\r\n    my @elements_array;\r\n    foreach my $origin_addr ( @{ $self-&gt;get_matrix_origin_addresses($matrix) } )\r\n    {\r\n        my $original_origin_addr = $original_origins-&gt;&#x5B; $origin_ct++ ];\r\n        $log-&gt;debug( 'Original origin address is : ' . $original_origin_addr );\r\n\r\n        #---Get results for current origination address\r\n        my $row = shift @{ $matrix-&gt;{rows} };\r\n\r\n        #------ Match origination address with all destination addresses\r\n        my $dest_ct = 0;\r\n        foreach my $destination_addr (\r\n            @{ $self-&gt;get_matrix_destination_addresses($matrix) } )\r\n        {\r\n\r\n            my $original_destination_addr =\r\n              $original_destinations-&gt;&#x5B; $dest_ct++ ];\r\n\r\n            $log-&gt;debug( 'Original destination address is : '\r\n                  . $original_destination_addr );\r\n\r\n           #----- get the result for the current Origination -&gt; Destination pair\r\n            my $element = shift @{ $row-&gt;{elements} };\r\n\r\n            push @elements_array,\r\n              {\r\n                origin_address               =&gt; $origin_addr,\r\n                destination_address          =&gt; $destination_addr,\r\n                original_origin_address      =&gt; $original_origin_addr,\r\n                original_destination_address =&gt; $original_destination_addr,\r\n                element_status               =&gt; $element-&gt;{status},\r\n                element_duration_text        =&gt; $element-&gt;{duration}{text},\r\n                element_duration_value       =&gt; $element-&gt;{duration}{value},\r\n                element_distance_text        =&gt; $element-&gt;{distance}{text},\r\n                element_distance_value       =&gt; $element-&gt;{distance}{value},\r\n              };\r\n        }\r\n    }\r\n    $log-&gt;debug(\r\n        'Array of all elements returned by Google:  ' . dump @elements_array );\r\n    return \\@elements_array;\r\n}\r\n<\/pre>\n<p class=\"important\">\nThis is far from being a completed masterpiece, but for now it will get me what I need. I intentionally didn&#8217;t allow for receiving the data in XML format, as Google recommends JSON format.I may add XML processing at a later date.I may even consider refactoring it from Moose to Moo, to make it lighter and nimbler.<br \/>\nI also plan to create a subclass class to Google::Travel::Time that will do the actual conversion from distance to a travel time acceptable to the NYC moving and storage industry. Until then, I don&#8217;t  consider this module to be CPAN worthy.\n<\/p>\n<p>\n<a href=\"https:\/\/twitter.com\/aibistin\" class=\"twitter-follow-button\" data-show-count=\"false\" >Follow @aibistin<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is my Perl Module Google::Travel::Matrix, which will be an interface to the Google Distance Matrix API. I will use this for calculating distance and travel times between multiple locations. Google Distance Matrix API Interface Google::Travel::Matrix This module provides an interface with the Google Distance Matrix API. It will return the distance between given locations [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[21,23,12,20,22],"class_list":["post-290","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dancer2","tag-htmlformhandler","tag-moose","tag-perl","tag-templatetoolkit"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/290","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=290"}],"version-history":[{"count":47,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions"}],"predecessor-version":[{"id":563,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=\/wp\/v2\/posts\/290\/revisions\/563"}],"wp:attachment":[{"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aibistin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}