How to check if position if valid in StreetView in Android

@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
    mPanorama.setOnStreetViewPanoramaChangeListener(new StreetViewPanorama.OnStreetViewPanoramaChangeListener() {
@Override
public void onStreetViewPanoramaChange(StreetViewPanoramaLocation streetViewPanoramaLocation) {
    if (streetViewPanoramaLocation != null && streetViewPanoramaLocation.links != null) {
        // location is present
    } else {
        // location not available
    }
}
 });

EDIT: Google's official answer is here (https://code.google.com/p/gmaps-api-issues/issues/detail?id=7033), which points here: https://code.google.com/p/gmaps-api-issues/issues/detail?id=4823

The official solution involves using the official Google Street View Image API (free, no limit - https://developers.google.com/maps/documentation/streetview/metadata) via HTTPS.

OLD (DEPRECATED) ANSWER:

I've hacked together a workaround for this that is ugly but stays within the API, until Google updates it to allow querying for panoramas the way the iOS SDK does.

This involves creating a StreetViewPanoramaView, but not attaching it to the layout, and setting its position to the current location. And then testing if it has a panorama location following that.

This seems to work, but will be filing a request on the Android Google Maps API V2 (Which is part of the google play services API) tracker to add this, since the iOS SDK has this capability.

// Handle on the panorama view
private StreetViewPanoramaView svpView;

...

// create a StreetViewPanoramaView in your fragment onCreateView or activity onCreate method
// make sure to handle its lifecycle methods with the fragment or activity's as per the documentation - onResume, onPause, onDestroy, onSaveInstanceState, etc.
StreetViewPanoramaOptions options = new StreetViewPanoramaOptions();
svpView = new StreetViewPanoramaView(getActivity(), options);
svpView.onCreate(savedInstanceState);

...

// Snippet for when the map's location changes, query for street view panorama existence
private Handler mHandler = new Handler();
private static final int QUERY_DELAY_MS = 500;

// When the map's location changes, set the panorama position to the map location
svpView.getStreetViewPanorama().setPosition(mapLocationLatLng);

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {
        if (svpView.getStreetViewPanorama().getLocation() != null) {
            // YOUR DESIRED ACTION HERE -- get the panoramaID, etc..
            // getLocation() -- a StreetViewPanoramaLocation -- will be null if there is no panorama
            // We have to delay this a bit because it may take some time before it loads
            // Note that adding an OnStreetViewPanoramaChangeListener doesn't seem to be reliably called after setPosition is called and there is a panorama -- possibly because it's not been added to the layout?
        }
    }
}, QUERY_DELAY_MS);

EDIT: to use the new Async API call:

svpView.getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback() {
    @Override
    public void onStreetViewPanoramaReady(final StreetViewPanorama panorama) {
        panorama.setPosition(mapLocationLatLng, DEFAULT_SEARCH_RADIUS);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (panorama.getLocation() != null) {
                    // your actions here
                }
            }
        }, QUERY_DELAY_MS);


I google a lot for this question. Finally I got one useful link. See there is no direct way to use getPanoramaByLocation() javascript function in android api. But there is a work around to this.

Here I'm providing url :

http://maps.googleapis.com/maps/api/streetview?size=400x400&location=40.720032,-73.988354&fov=90&heading=235&pitch=10

See cases :

  1. When you hit this url by providing valid lat/lng. You will get streetview. So you can use that image size or contents for checking whether its a blank image or valid image.

See this url :

http://maps.googleapis.com/maps/api/streetview?size=400x400&location=73.67868,-73.988354&fov=90&heading=235&pitch=10

  1. In above url I passed invalid lat/lng means this for this lat/lng there is no streetview so you can either use "text extract library" or by checking file size. You will get to know whether streetview is available or not for that point

Check this link for response when you hit url for fetching streetview :

https://developers.google.com/maps/documentation/geocoding/?hl=nl