Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android StreetView check if there is any view for given location

I'm just playing around with the new StreetView feature of the Google Play Services 4.4 for Android (link) and I was wondering if there is any method /possibility to check if there is a 'view'/ foto material for any given location. When I load a location that isn't covered by StreetView with streetViewPanorama.setPosition(someLatLng); I just get a black screen. Any way to check beforehand?

like image 314
fweigl Avatar asked Nov 29 '22 11:11

fweigl


2 Answers

Black screen appears when location is not present. Just check whether it was loaded or not.

@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
            }
        }
    });
like image 159
Bartosz Mądry Avatar answered Dec 15 '22 21:12

Bartosz Mądry


@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
        }
    }
});

looking at the documentation I believe this should do it

StreetViewPanoramaLocation location = mSvp.getLocation();
if (location != null && location.links != null) {
    mSvp.setPosition(location.links[0].panoId);
}

it is my understanding that if location is null or location.links (links is the array of street view images) is null then it is safe to say there is no street view at that position

referencing from this link

https://developers.google.com/maps/documentation/android/streetview

Edit

testing this out there is a caveat to this method, you have to wait until the view has been created. so for example doing this inside the fragment itself in the onCreateView I put in a handler with a 1 second delay to test out this theory.

Handler h = new Handler();
h.postDelayed(new Runnable(){

    @Override
    public void run() {
        StreetViewPanoramaLocation svpl = mSvp.getLocation();

        if(svpl == null){
            Toast.makeText(getActivity(), "Unable to show Street View at this location", Toast.LENGTH_SHORT).show();
        }
    }

}, 1000);

if you try to use the getLocation() before the view has been created it will always return null. Accessing it after will return null if there is no street view at those coordinates

Edit 2:

there is now a callback that you should use to let you know when the streetview is ready

getStreetViewPanoramaAsync(new OnStreetViewPanoramaReadyCallback(){
    @Override
    public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {

    }
})

https://developers.google.com/android/reference/com/google/android/gms/maps/StreetViewPanoramaView.html#getStreetViewPanoramaAsync(com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback)

like image 25
tyczj Avatar answered Dec 15 '22 22:12

tyczj