Getting the POV for Google StreetView API

前端 未结 6 1016
抹茶落季
抹茶落季 2020-12-04 21:26

I am currently using the Google Street View Panorama embed and I want to get the POV (more specifically heading) for a particular address.

Google does this via maps.

6条回答
  •  难免孤独
    2020-12-04 22:15

    The reason for this is that the street view POV is, by default the direction the truck was facing when the image was shot (go figure). You need to get the location of the truck and the location of the house and calculate a "heading" from the first location to the second:

    // adrloc=target address
    // svwloc=street-view truck location
    svwService.getPanoramaByLocation(adrloc,svwdst,function(dta,sts) {
        if(sts==google.maps.StreetViewStatus.OK) {
            var svwloc=dta.location.latLng;
            var svwhdg=google.maps.geometry.spherical.computeHeading(svwloc,adrloc);
            var svwmap=avwMap.getStreetView();
            svwmap.setPosition(svwloc);
            svwmap.setPov({ heading: svwhdg, pitch: 0 });
            svwMarker=new google.maps.Marker({ map:svwmap, position: adrloc });
            svwmap.setVisible(true);
            }
        else {
            ...
            }
    

    Another trick/trap using street view is that you need to obtain the closest street view to your address location by repeatedly calling getPanoramaByLocation with an increasing distance until you are either successful or reach some maximum distance. I solve this using this code:

    var SVW_MAX=100; // maximum street-view distance in meters
    var SVW_INC=10;  // increment street-view distance in meters
    var svwService=new google.maps.StreetViewService(); // street view service
    var svwMarker=null; // street view marker
    
    // NOTE: avwMap is the aerial view map, code not shown
    ...
    resolveStreetView(avwMap.getCenter(),SVW_INC); 
    ...
    
    var resolveStreetView=function(adrloc,svwdst) {
        svwService.getPanoramaByLocation(adrloc,svwdst,function(dta,sts) {
            if(sts==google.maps.StreetViewStatus.OK) {
                var svwloc=dta.location.latLng;
                var svwhdg=google.maps.geometry.spherical.computeHeading(svwloc,adrloc);
                var svwmap=avwMap.getStreetView();
                svwmap.setPosition(svwloc);
                svwmap.setPov({ heading: svwhdg, pitch: 0 });
                svwMarker=new google.maps.Marker({ map:svwmap, position: adrloc });
                svwmap.setVisible(true);
                }
            else if(svwdst

提交回复
热议问题