[PHP] [Maps V3] Cheking if an StreetView panorama exists for a location

Introduction

Last days, while working in a project I’ve got the need to check if a streetview panorama was available for an exact address or a LatLng location for showing (or not) an streetview panorama in a webpage.

Reading and checking the API for StreetView (https://developers.google.com/maps/documentation/javascript/streetview) and having the need to check the existence or not of a panorama using PHP, I began to observe Google Maps behavior and HTTP Requests.

While moving the StreetView symbol (the orange man), I realised that Google Maps was making a request to a service wich returned the data of the panorama or an empty document in case that a panorama for the location isn’t available. Discovering that request, gave me the opportunity to know exactly what I want and let me implement the behavior that the application needed without showing the customer a gray image that StreetView displays when no panorama is available at the selected location.

Endpoint Calls to check if a StreetView panorama is available

Google Maps makes requests to the following endpoint of their API to check whether a StreetView panorama is available or not:

http://maps.google.com/cbk?output=xml&hl=en&ll=39.470611,-0.3899&radius=50&cb_client=maps_sv&v=4

In the endpoint some parameters could be observed:

  • output: The format of the result, that can be only XML or JSON (we’ll choose json for easier processing with PHP)
  • hl: The language of the request (in this case en means English)
  • ll: The exact pair Latitude-Longitude separated by a comma
  • radius:  This parameter is something like a “distance from the point to the observer” that the point can be observable and have a StreetView panorama available.  The lower this value is the closest view that can be obtained for a StreetView panorama (0, zero in this case). The default value seems 50, and the units seems that are in meters.
  • cb_client& v: The client that requests the data and the version of that client.

Calling the endpoint in PHP

For calling this endpoint we’ll be using PHP 5.2+ and the Curl Library . Be sure to have this library installed in your developing machine or your hosting (in Ubuntu is as easy to install as typing sudo apt-get install php5-curl ). After installed or a test that shows Curl is working properly, we’ll write some code to make an HTTP Request to the explained endpoint and check if a panorama is available or not.

$endpoint = "http://maps.google.com/cbk?output=json&hl=en&ll=39.470611,-0.3899&radius=10&cb_client=maps_sv&v=4"
$handler = curl_init();
curl_setopt($handler, CURLOPT_HEADER, 0);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handler, CURLOPT_URL, $endpoint);
$data = curl_exec($handler);
curl_close($handler);
// if data value is an empty json document ('{}') , the panorama is not available for that point
if ($data==='{}') {
print "StreetView Panorama isn't available for the selected location";
}

I know the panorama is available but this returns it isn’t!

If checking only with a value of 10 in the radius parameter of the endpoint doesn’t gets a positive response, be trying some values for that parameter. I’ll recommend from 10 to 75 in steps of 10, 25, 50 and 75. But think that a StreetView panorama from 50 meters (or more) could be far away from the point that you need to show, and you need exactly that point, not a view from 50 meters of that point.

Conclussions

Is possible to check if Google has a StreetView panorama of an exact LatLng location with one meter radius precision with little effort and this trick.

How is possible that Google hasn’t put any method on the API to check that existence?  Can Google add some information in a Google Maps V3 Geocoder that could tell a developer of that existence?  It’ll be a lot easier by only checking the data received of the Geocoder and not having to do this trick.

Thanks for reading.
If you have any questions or have more information/another way to check existence in PHP/JS, please don’t mind to contact me using the comments of this post.

Andrés Ignacio Martínez Soto