How can I tell if Google's Streetview Image API Returns “Sorry, we have no imagery here” (ie. NULL) Result?

后端 未结 8 671
清酒与你
清酒与你 2020-12-03 07:38

The Google Street View Image API lets you embed a static (non-interactive) Street View panorama or thumbnail into your web page, without the use of JavaScript.

Reque

8条回答
  •  忘掉有多难
    2020-12-03 08:12

    Similar to MALK:

    Another way is to load the image and then compare some pixels colors. The "no streetview" image from google is always the same. Here is how you would compare 2 pixels:

    var url = STREETVIEWURL
    var img = new Image();
    // Add some info to prevent cross origin tainting
    img.src = url + '?' + new Date().getTime();
    img.setAttribute('crossOrigin', '');
    img.crossOrigin = "Anonymous";
    img.onload = function() {
        var context = document.createElement('CANVAS').getContext('2d');
        context.drawImage(img, 0, 0);
        //load 2 pixels.  I chose the first one and the 5th row
        var data1 = context.getImageData(0, 0, 1, 1).data;
        var data2 = context.getImageData(0, 5, 1, 1).data;
        console.log(data1);
        // google unknown image is this pixel color [228,227,223,255]
        if(data1[0]==228 && data1[1]==227 && data1[2]==223 && data1[3]==255 && 
                         data2[0]==228 && data2[1]==227 && data2[2]==223 && data2[3]==255){
            console.log("NO StreetView Available");
        }else{
             console.log("StreetView is Available");
        }
    };
    

    Some potential issues: I've seen some errors with CrossOrigin tainting. Also, if google changes the image returned this code will break.

提交回复
热议问题