My application is matching people based on distance. The distance is calculated with the Google Maps API Distance Matrix. It's grouping the locations of the people and then making 1 query with the API.
Example:
FROM: New York Los Angeles Chicago New York New York Los Angeles
TO: Los Angeles San Diego Dallas Dallas Dallas Austin
The array will be: FROM: New York Los Angeles Chicago
TO: Los Angeles San Diego Dallas Austin
Using the Google API for the same cities every refresh can be optimised. So I was thinking about creating a table that holds the cities.
CREATE TABLE IF NOT EXISTS `distancematrix` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from` varchar(100) NOT NULL,
`to` varchar(100) NOT NULL,
`distancetime` int(11) NOT NULL,
`distancekm` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `from` (`from`),
KEY `to` (`to`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
Now I want to create a library (I'm using CodeIgniter) that first checks the table, if there are no results it should use the Google API and store the result for the next time.
How would you code a function like this? It must be efficient, therefor I'm seeking advice. I don't want to have unnecessary loops obviously.
The function should be called by just throwing a from and a to array in it.
The function for the table:
public function get_distance($from, $to) {
$result_array = array();
if (is_array($from)) {
foreach($from as $index => $origin) {
$query = $this->db->select("`from` as `origin`, `to` as `destination`, distancetime, distancekm, 'true' as result", false)
->from('distancematrix')
->where('from', $origin)
->where_in('to', $to);
$query_result = $query->get()->result_array();
$opposite = $this->db->select("`from` as `destination`, `to` as `origin`, distancetime, distancekm, 'true' as result", false)
->from('distancematrix')
->where_in('from', $to)
->where('to', $origin);
$opposite_result = $opposite->get()->result_array();
$merge_result = array_merge($query_result, $opposite_result);
$result_array[$origin] = $merge_result;
}
}
return $result_array;
}
The options I see: 1. Do a query for every combination, if it is not found, put it in a not found array. This not found array is later used with API.
- Not sure yet...
What options do you see?
Thanks in advance!