public function testViewportBiasing() { $location = 'Winnetka'; $swLat = 34.172684; $swLng = -118.604794; $neLat = 34.236144; $neLng = -118.500938; $params = array('language' => 'en', 'sensor' => 'false'); $format = 'json'; $params = array_merge($params, array('bounds' => "{$swLat},{$swLng}|{$neLat},{$neLng}")); $params = array_merge($params, array('address' => $location)); $this->service->biasViewport($swLat, $swLng, $neLat, $neLng); $this->assertEquals("http://maps.googleapis.com/maps/api/geocode/{$format}?" . http_build_query($params, '', '&'), $this->service->generateUrl($location, $format), 'URLs did not match'); $this->service->removeViewportBias(); $bounds = new stdClass(); $bounds->southwest = new stdClass(); $bounds->northeast = new stdClass(); $bounds->southwest->lat = $swLat; $bounds->southwest->lng = $swLng; $bounds->northeast->lat = $neLat; $bounds->northeast->lng = $neLng; $this->service->biasViewportByBoundsObject($bounds); $this->assertEquals("http://maps.googleapis.com/maps/api/geocode/{$format}?" . http_build_query($params, '', '&'), $this->service->generateUrl($location, $format), 'URLs did not match'); }
<?php /** * This example shows * * - How to perform a reverse geocode * - How to determine the city of the location */ require '../sdk/bootstrap.php'; require '../sdk/communicator/CurlCommunicator.php'; $service = new GoogleGeocodeServiceV3(new CurlCommunicator()); // Geographic center of US ZIP code 90210 $response = $service->reverseGeocode(34.1346702, -118.4389877); while ($response->valid()) { // Address component type we're checking for $component = GoogleGeocodeResponseV3::ACT_LOCALITY; // Is it a city-level result? if ($response->assertType($component)) { // Get the city name echo $response->getAddressComponentName($component); break; } $response->next(); } echo '<hr>', highlight_file(__FILE__, 1);
<?php /** * This example shows * * - How to bias a response towards a region * - How to bias a response towards a viewport */ require '../sdk/bootstrap.php'; require '../sdk/communicator/CurlCommunicator.php'; $service = new GoogleGeocodeServiceV3(new CurlCommunicator()); // Show that the API assumes "Portland, OR" for "Portland USA" echo $service->geocode('Portland USA')->getFormattedAddress(), '<br>'; // Now geocode the state of Maine and bias results to its viewport $maine = $service->geocode('Maine, USA'); $service->biasViewportByBoundsObject($maine->getViewport()); // Re-geocode "Portland USA" echo $service->geocode('Portland USA')->getFormattedAddress(), '<br>'; // Establish an ambiguous location $location = 'Toledo'; // Bias for the USA $service->biasRegion('com'); echo $service->geocode($location)->getFormattedAddress(), '<br>'; // Bias for Spain $service->biasRegion('es'); echo $service->geocode($location)->getFormattedAddress(), '<br>'; echo '<hr>', highlight_file(__FILE__, 1);
<?php /** * This example shows * * - How the results can be counted * - How to iterate over a response with multiple results * - How to pull a specific address component name from a result */ require '../sdk/bootstrap.php'; require '../sdk/communicator/CurlCommunicator.php'; $service = new GoogleGeocodeServiceV3(new CurlCommunicator()); // Deliberately geocode an address that will yield multiple results $response = $service->geocode('Springfield'); // Remember, GoogleGeocodeResponseV3 implements the Countable interface if (count($response) > 1) { echo "The city of Springfield was found in: <br>"; while ($response->valid()) { // Get the State name echo $response->getAddressComponentName(GoogleGeocodeResponseV3::ACT_ADMINISTRATIVE_AREA_LEVEL_1), '<br>'; $response->next(); } } else { echo "Only one or no results"; } echo '<hr>', highlight_file(__FILE__, 1);