我使用Google Street View image API來顯示位置圖像。如何檢查Google街景圖片API是否返回圖片?
它工作正常,但是當沒有圖片可用我得到一個黑色的圖像,而不是位置圖片。有沒有什麼方法可以檢查是否沒有圖像被返回並顯示另一個圖像?
我使用Google Street View image API來顯示位置圖像。如何檢查Google街景圖片API是否返回圖片?
它工作正常,但是當沒有圖片可用我得到一個黑色的圖像,而不是位置圖片。有沒有什麼方法可以檢查是否沒有圖像被返回並顯示另一個圖像?
我不認爲有一種方法來檢查這一點。 SV圖像API是針對靜態情況設計的,當您無法向頁面添加更多功能時。
也許你應該看看Street View in the JS API,它可以讓你檢測圖像是否可用。
另一種方法是加載圖像,然後比較一些像素的顏色。谷歌的「no streetview」圖片總是一樣的。下面是你將如何比較2個像素:
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");
}
};
一些潛在的問題:我已經看到CrossOrigin污點的一些錯誤。另外,如果谷歌改變了圖像返回這個代碼將打破。
//Image Div
<div id='pano'>
streetview.getPanoramaByLocation(streetViewLocation, 50, function(data, status) {
if (status == 'OK') {
var fenway = {lat: Number(alarm_lat), lng: Number(alarm_lng)};
var panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), {
position: fenway,
pov: {
heading: 34,
pitch: 10
}
});
map.setStreetView(panorama);
}
else{
//another image here
}
});
不確定爲什麼有投票結束,因爲這在我看來似乎是一個非常明智的問題 - 儘管看起來第一眼看不到一個簡單的答案。 – Murph