Android StreetView check if there is any view for given location

前端 未结 2 644
孤城傲影
孤城傲影 2020-12-10 20:08

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

相关标签:
2条回答
  • 2020-12-10 20:46
    @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)

    0 讨论(0)
  • 2020-12-10 20:55

    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
                }
            }
        });
    
    0 讨论(0)
提交回复
热议问题