| 1 | <?php |
||
| 2 | |||
| 3 | // Test this example by executing: php -S localhost:2626 |
||
| 4 | // Open your favorite browser and go to http://localhost:2626/example.php |
||
| 5 | |||
| 6 | ini_set('display_errors', true); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 7 | error_reporting(E_ALL); |
||
| 8 | |||
| 9 | require_once __DIR__.'/vendor/autoload.php'; |
||
| 10 | |||
| 11 | echo '<h1>Google Street view example</h1>'; |
||
| 12 | |||
| 13 | $dotEnv = new \Dotenv\Dotenv(__DIR__); |
||
| 14 | $dotEnv->load(); |
||
| 15 | |||
| 16 | $apiKey = getenv('GOOGLE_API_KEY'); |
||
| 17 | if (!$apiKey || empty($apiKey)) { |
||
| 18 | throw new RuntimeException('No Google api key found.'); |
||
| 19 | } |
||
| 20 | |||
| 21 | $client = new \GuzzleHttp\Client(); |
||
| 22 | |||
| 23 | $streetView = new \Defro\Google\StreetView\Api($client); |
||
| 24 | $streetView->setApiKey($apiKey); |
||
| 25 | |||
| 26 | $locationName = 'Sydney Opera House, Sydney, Australia'; |
||
| 27 | $locationName = 'Forbidden City, Beijing, China'; |
||
| 28 | //$locationName = 'A place which not exists'; |
||
| 29 | |||
| 30 | echo '<h2>Handle location: "'.$locationName.'"</h2>'; |
||
| 31 | |||
| 32 | try { |
||
| 33 | echo '<h3>Print METADATA collect by location name</h3>'; |
||
| 34 | $metadata = $streetView->getMetadata($locationName); |
||
| 35 | echo '<ol>'; |
||
| 36 | foreach ($metadata as $key => $value) { |
||
| 37 | printf('<li>%s: %s</li>', $key, $value); |
||
| 38 | } |
||
| 39 | echo '</ol>'; |
||
| 40 | } catch (Exception $e) { |
||
| 41 | exit('Error when get metadata : '.$e->getMessage()); |
||
| 42 | } |
||
| 43 | |||
| 44 | $streetView->setImageWidth(400)->setImageHeight(400); |
||
| 45 | |||
| 46 | try { |
||
| 47 | echo '<h3>Display image url by location name</h3>'; |
||
| 48 | $imgUrl = $streetView->getImageUrlByLocation($locationName); |
||
| 49 | printf('<p><a href="https://www.google.com/maps/search/?api=1&query=%s"><img src="%s" alt="%s" /></a></p>', urlencode($locationName), $imgUrl, $locationName); |
||
| 50 | } catch (Exception $e) { |
||
| 51 | exit('Error when get image url by location name : '.$e->getMessage()); |
||
| 52 | } |
||
| 53 | |||
| 54 | try { |
||
| 55 | echo '<h3>Display image url by panorama ID</h3>'; |
||
| 56 | $imgUrl = $streetView->getImageUrlByPanoramaId($metadata['panoramaId']); |
||
| 57 | printf('<p><a href="https://www.google.com/maps/@?api=1&map_action=pano&pano=%s"><img src="%s" alt="%s" /></a></p>', $metadata['panoramaId'], $imgUrl, $metadata['panoramaId']); |
||
| 58 | } catch (Exception $e) { |
||
| 59 | exit('Error when get image url by panorama ID : '.$e->getMessage()); |
||
| 60 | } |
||
| 61 | |||
| 62 | try { |
||
| 63 | echo '<h3>Display image url by latitude and longitude</h3>'; |
||
| 64 | $imgUrl = $streetView->getImageUrlByLatitudeAndLongitude($metadata['lat'], $metadata['lng']); |
||
| 65 | printf('<p><a href="https://www.google.com/maps/@?api=1&map_action=pano&viewpoint=%s,%s"><img src="%s" alt="%s" /></a></p>', $metadata['lat'], $metadata['lng'], $imgUrl, $metadata['panoramaId']); |
||
| 66 | } catch (Exception $e) { |
||
| 67 | exit('Error when get image url by latitude and longitude : '.$e->getMessage()); |
||
| 68 | } |
||
| 69 |