Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 239
wleftwich Avatar asked Nov 05 '22 08:11

wleftwich


1 Answers

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);
};
like image 57
wleftwich Avatar answered Nov 13 '22 03:11

wleftwich