detecting “we have no imagery” of google maps street view static images

后端 未结 6 1710
梦毁少年i
梦毁少年i 2021-02-13 17:43

I\'m generating street view static images like so:

https://maps.googleapis.com/maps/api/streetview?size=1080x400&location=%s&fov=90&heading=235&pitch=0&a

6条回答
  •  粉色の甜心
    2021-02-13 18:34

    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.

提交回复
热议问题