protected function _init() { parent::_init(); $parse = function ($data) { if (!is_array($data)) { $data = json_decode($data, true); } if (!isset($data['results'][0])) { return false; } $result = $raw = $data['results'][0]; if (isset($raw['geometry']['bounds'])) { $bounds = $raw['geometry']['bounds']; $result += array('bounds' => array('box' => array(array('latitude' => $bounds['southwest']['lat'], 'longitude' => $bounds['southwest']['lng']), array('latitude' => $bounds['northeast']['lat'], 'longitude' => $bounds['northeast']['lng'])))); } if (isset($raw['geometry']['location'])) { $result += array('coordinates' => array('latitude' => $raw['geometry']['location']['lat'], 'longitude' => $raw['geometry']['location']['lng'])); } $keys = array('title' => 'point_of_interest', 'number' => 'street_number', 'street' => 'route', 'neighborhood' => array('neighborhood', 'sublocality'), 'city' => 'locality', 'county' => 'administrative_area_level_2', 'state' => 'administrative_area_level_1', 'province' => 'administrative_area_level_1', 'postalCode' => 'postal_code', 'country' => 'country'); if (isset($raw['address_components'])) { $addr = $raw['address_components']; $map = function ($key) use($addr) { $value = null; if (is_array($key)) { foreach ($key as $test) { foreach ($addr as $component) { if (in_array($test, $component['types'])) { $value = $component['long_name']; break 2; } } } } else { foreach ($addr as $component) { if (in_array($key, $component['types'])) { $value = $component['long_name']; break; } } } if ($value === null) { return null; } return Geocoder::normalizePlace($value); }; $address = array_filter(array_map($map, $keys)); $continent = Geocoder::continents($address['country']); $address['continent'] = is_array($continent) ? $address['country'] : $continent; $result += compact('address'); } return $result; }; $this->_parsers += array('coords' => $parse, 'address' => $parse); }
public function testDistanceCalculation() { $a = array(40.625316, -74.025972); $b = array(40.755473, -73.98085500000001); $distance = Geocoder::distance($a, $b); $this->assertEqual(9.298, round($distance, 3)); $distance = Geocoder::distance($a, $b, 'F'); $this->assertEqual(49093, round($distance)); $b = array('latitude' => 44.7525199, 'longitude' => -93.244557); $distance = Geocoder::distance($a, $b, 'K'); $this->assertEqual(1632, round($distance)); }
protected function _init() { parent::_init(); $this->_parsers += array('coords' => function ($data) { $result = json_decode($data, true); if (empty($result)) { return false; } $raw = $result[0]; $bounds = array_map('floatval', $raw['boundingbox']); return compact('raw') + array('coordinates' => array('latitude' => floatval($raw['lat']), 'longitude' => floatval($raw['lon'])), 'license' => $raw['licence'], 'bounds' => array('box' => array(array('latitude' => $bounds[0], 'longitude' => $bounds[2]), array('latitude' => $bounds[1], 'longitude' => $bounds[3])))); }, 'address' => function ($data) { $result = json_decode($data, true); if (empty($result)) { return false; } $raw = $result + array('address' => array(), 'licence' => null); $addr = $raw['address']; $keys = array('title' => 'attraction', 'number' => 'house_number', 'street' => 'pedestrian', 'neighborhood' => 'suburb', 'city' => 'city', 'county' => 'county', 'state' => 'state', 'province' => 'province', 'postalCode' => 'postcode', 'country' => 'country'); $map = function ($key) use($addr) { if (is_array($key)) { foreach ($key as $test) { if (isset($addr[$test])) { $key = $test; break; } } if (is_array($key)) { return null; } } return isset($addr[$key]) ? Geocoder::normalizePlace($addr[$key]) : null; }; $address = array_filter(array_map($map, $keys)); $continent = Geocoder::continents($address['country']); $address['continent'] = is_array($continent) ? $address['country'] : $continent; return compact('raw', 'address') + array('license' => $raw['licence'], 'coordinates' => array('latitude' => floatval($raw['lat']), 'longitude' => floatval($raw['lon']))); }); }
protected function _init() { parent::_init(); $parse = function ($data) { $data = json_decode($data, true); if (!isset($data['results'][0])) { return false; } $result = $raw = $data['results'][0]; // there are times when certain queries span 2 areas // when this happens, google will return more than one result // sometimes, vital information (like a zip code) will not be in // the first result. if (count($data['results']) > 1) { $key = 'address_components'; foreach ($data['results'] as $result) { if (empty($result[$key])) { continue; } if (empty($raw[$key])) { $raw[$key] = array(); } $raw[$key] = array_merge($raw[$key], $result[$key]); } } if (isset($raw['geometry']['bounds'])) { $bounds = $raw['geometry']['bounds']; $result += array('bounds' => array('box' => array(array('latitude' => $bounds['southwest']['lat'], 'longitude' => $bounds['southwest']['lng']), array('latitude' => $bounds['northeast']['lat'], 'longitude' => $bounds['northeast']['lng'])))); } if (isset($raw['geometry']['location'])) { $result += array('coordinates' => array('latitude' => $raw['geometry']['location']['lat'], 'longitude' => $raw['geometry']['location']['lng'])); } $keys = array('title' => 'point_of_interest', 'number' => 'street_number', 'street' => 'route', 'neighborhood' => array('neighborhood', 'sublocality'), 'city' => 'locality', 'county' => 'administrative_area_level_2', 'state' => 'administrative_area_level_1', 'province' => 'administrative_area_level_1', 'postalCode' => 'postal_code', 'country' => 'country'); if (isset($raw['address_components'])) { $addr = $raw['address_components']; $map = function ($key) use($addr) { $value = null; if (is_array($key)) { foreach ($key as $test) { foreach ($addr as $component) { if (in_array($test, $component['types'])) { $value = $component['long_name']; break 2; } } } } else { foreach ($addr as $component) { if (in_array($key, $component['types'])) { $value = $component['long_name']; break; } } } if ($value === null) { return null; } return Geocoder::normalizePlace($value); }; $address = array_filter(array_map($map, $keys)); $continent = Geocoder::continents($address['country']); $address['continent'] = is_array($continent) ? $address['country'] : $continent; $result += compact('address'); } return $result; }; $this->_parsers += array('coords' => $parse, 'address' => $parse); }