public static function castToBounds($input) { try { return Bounds::normalize($input); } catch (\InvalidArgumentException $e) { } try { $latLng = LatLng::normalize($input); return new Bounds($latLng, $latLng); } catch (\InvalidArgumentException $e) { } throw new \InvalidArgumentException(sprintf('Cannot cast to Bounds from input %s.', json_encode($input))); }
private function normalize_point_to_lat_lng($point) { if (is_string($point) && ereg("^(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3})?\$", $point)) { return $this->geocode_ip_address($point); } else { return LatLng::normalize($point); } }
/** * Calculates the destination point along a geodesic, given an initial * heading and distance, from the given start point. * * @see http://www.movable-type.co.uk/scripts/latlong.html * @param mixed $start * @param float $heading (in degrees) * @param mixed $distance (in meters) * @return LatLng */ public function endpoint($start, $heading, $distance) { $start = LatLng::normalize($start); $distance = Distance::normalize($distance); $lat = deg2rad($start->getLatitude()); $lng = deg2rad($start->getLongitude()); $angularDistance = $distance->meters() / $this->ellipsoid->getSemiMajorAxis(); $heading = deg2rad($heading); $lat2 = asin(sin($lat) * cos($angularDistance) + cos($lat) * sin($angularDistance) * cos($heading)); $lon2 = $lng + atan2(sin($heading) * sin($angularDistance) * cos($lat), cos($angularDistance) - sin($lat) * sin($lat2)); return new LatLng(rad2deg($lat2), rad2deg($lon2)); }
public function normalize($thing, $other = null) { # maybe this will be simple -- # If an actual bounds object is passed, we can all go home if ($thing instanceof Bounds) { return $thing; } # If there's no $other, $thing had better be a two-element array if (!$other && is_array($thing) && count($thing) == 2) { $other = $thing[1]; $thing = $thing[0]; } # Now that we're set with a thing and another thing, # let LatLng do the heavy lifting. # Exceptions may be thrown return new Bounds(LatLng::normalize($thing), LatLng::normalize($other)); }
/** * Takes anything which looks like bounds and generates a Bounds object * from it. * * $input can be either a string, an array, an \ArrayAccess object or a * Bounds object. * * If $input is a string, it can be in the format * "1.1234, 2.5678, 3.910, 4.1112" or "1.1234 2.5678 3.910 4.1112". * * If $input is an array or \ArrayAccess object, it must have a south-west * and east-north entry. * * Recognized keys are: * * * South-west: * * southwest * * south_west * * southWest * * * North-east: * * northeast * * north_east * * northEast * * If $input is an indexed array, it assumes south-west at index 0 and * north-east at index 1, eg. [[-45.0, 180.0], [45.0, -180.0]]. * * If $input is an Bounds object, it is just passed through. * * @param mixed $input * @return Bounds * @throws \InvalidArgumentException */ public static function normalize($input) { if ($input instanceof self) { return $input; } $southWest = null; $northEast = null; if (is_string($input) && preg_match('/(\\-?\\d+\\.?\\d*)[, ] ?(\\-?\\d+\\.?\\d*)[, ] ?(\\-?\\d+\\.?\\d*)[, ] ?(\\-?\\d+\\.?\\d*)$/', $input, $match)) { $southWest = array('lat' => $match[1], 'lng' => $match[2]); $northEast = array('lat' => $match[3], 'lng' => $match[4]); } elseif (is_array($input) || $input instanceof \ArrayAccess) { if (Utils::isNumericInputArray($input)) { $southWest = $input[0]; $northEast = $input[1]; } else { $southWest = Utils::extractFromInput($input, self::$southWestKeys); $northEast = Utils::extractFromInput($input, self::$northEastKeys); } } if (null !== $southWest && null !== $northEast) { try { return new self(LatLng::normalize($southWest), LatLng::normalize($northEast)); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException(sprintf('Cannot normalize Bounds from input %s.', json_encode($input)), 0, $e); } } throw new \InvalidArgumentException(sprintf('Cannot normalize Bounds from input %s.', json_encode($input))); }
public function testNormalizeShouldAcceptIndexedArrayArgument() { $LatLng = LatLng::normalize(array(2.5678, 1.1234)); $this->assertSame(2.5678, $LatLng->getLatitude()); $this->assertSame(1.1234, $LatLng->getLongitude()); }
function test_normalize() { $lat = 37.769; $lng = -122.443; $res = LatLng::normalize($lat, $lng); $this->assertEqual($res, new LatLng($lat, $lng)); $res = LatLng::normalize($lat . ',' . $lng); $this->assertEqual($res, new LatLng($lat, $lng)); $res = LatLng::normalize($lat . ' ' . $lng); $this->assertEqual($res, new LatLng($lat, $lng)); $res = LatLng::normalize((int) $lat . ' ' . (int) $lng); $this->assertEqual($res, new LatLng((int) $lat, (int) $lng)); $res = LatLng::normalize(array($lat, $lng)); $this->assertEqual($res, new LatLng($lat, $lng)); }