Check StreetView availability with Google Maps JavaScript API V3
I've got a webapp that uses Google Maps JavaScript API V3 to display a regular googlemap and a StreetView side by side. When the map changes position, it tells the streetview to follow it using StreetViewPanorama.setPosition().
However, when I scroll the map to someplace where StreetView is not available, the streetview image stays stuck开发者_StackOverflow中文版 at the last location. Its getPosition() method returns the same LatLng as the master map.
How can I tell if I have moved to a place where StreetView is not available?
OK, I found an answer, if not the answer.
After each move, use StreetViewService.getPanoramaByLocation() to get the nearest panorama within N meters. Based on that you can stay where you are, move, or setVisible(false).
I used a flag and a setTimer to prevent lots of unnecessary calls to getPanoramaByLocation like this:
var check_availability_lock = false;
var check_availability = function() {
if (check_availability_lock) {
return;
}
check_availability_lock = true;
var availability_cb = function(data, status) {
check_availability_lock = false;
// console.log("status = ", status);
if (status !== 'OK') {
map.setVisible(false);
}
else {
map.setVisible(true);
}
}
setTimeout(function(){
var latlng = map.getPosition();
svc.getPanoramaByLocation(latlng, 50, availability_cb);
}, 2000);
};
精彩评论