谷歌街景JS给我一个房子的侧面而不是前面的视图

时间:2015-05-21 17:32:03

标签: javascript google-maps google-maps-api-3 google-street-view

使用Google的示例和Stack Overflow上的其他代码中的示例,我汇总了一些带有街道地址并显示高架地图和街景视图的JS。在大多数情况下,这些工作非常好,但是当房子在拐角处时它就会破裂。

当房子在拐角处时,它有时会显示房子的一侧而不是房子的前面。 If I go directly to Google Maps and search for that address, the Street View it shows is actually the front of the house,所以我知道必须有办法确定正确使用的视图。

如何让我的代码显示在Google网站上的房子前面?

function load_map_and_street_view_from_address(address) {
    // Check if GPS has been locally cached.
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode( { 'address': address }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {

            var gps = results[0].geometry.location;
            create_map_and_streetview(gps.lat(), gps.lng(), 'map_canvas', 'pano');
        }
    });
}

function  create_map_and_streetview(lat, lng, map_id, street_view_id) {

    var panorama = new google.maps.StreetViewPanorama(document.getElementById(street_view_id));
    var addLatLng = new google.maps.LatLng(lat,lng);
    var service = new google.maps.StreetViewService();
    service.getPanoramaByLocation(addLatLng, 50, function(panoData, status) {
        if (status != google.maps.StreetViewStatus.OK) {
            $('#pano').html('No StreetView Picture Available').attr('style', 'text-align:center;font-weight:bold').show();
            return;
        }
        var angle = google.maps.geometry.spherical.computeHeading(panoData.location.latLng, addLatLng);

        var panoOptions = {
            position: addLatLng,
            addressControl: false,
            linksControl: false,
            panControl: false,
            zoomControlOptions: {
                style: google.maps.ZoomControlStyle.SMALL
            },
            pov: {
                heading: angle,
                pitch: 0,
                zoom: 1
            },
            enableCloseButton: false,
            visible:true
        };

        panorama.setOptions(panoOptions);
    });

    var myOptions = {
        zoom: 14,
        center: addLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        backgroundColor: 'transparent',
        streetViewControl: false,
        keyboardShortcuts: false
    };
    var map = new google.maps.Map(document.getElementById(map_id), myOptions);
    var marker = new google.maps.Marker({
        map:map,
        animation: google.maps.Animation.DROP,
        position: addLatLng
    });
}

$(document).ready(function() {
    console.log('ready');
    load_map_and_street_view_from_address("2131 S SAN ANTONIO AVE, ONTARIO, CA 91762");
});

https://jsfiddle.net/kennywyland/xm59cbac/

1 个答案:

答案 0 :(得分:2)

您正在将全景设置到错误的位置。你想要它在房子的前面(对着公路,使用路线服务),但指向地理编码器的结果。

{{3}}

function create_map_and_streetview(addLatLng, panoLatLng, map_id, street_view_id) {

    var panorama = new google.maps.StreetViewPanorama(document.getElementById(street_view_id));
    var service = new google.maps.StreetViewService();
    service.getPanoramaByLocation(panoLatLng, 50, function (panoData, status) {
        if (status != google.maps.StreetViewStatus.OK) {
            $('#pano').html('No StreetView Picture Available').attr('style', 'text-align:center;font-weight:bold').show();
            return;
        }
        var angle = google.maps.geometry.spherical.computeHeading(panoData.location.latLng, addLatLng);
        document.getElementById('angle').innerHTML = angle;
        var panoOptions = {
            position: panoLatLng,
            pov: {
                heading: angle,
                pitch: 0,
                zoom: 1
            },
            enableCloseButton: false,
            visible: true
        };

        panorama.setOptions(panoOptions);
    });

    var myOptions = {
        zoom: 16,
        center: addLatLng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        backgroundColor: 'transparent',
        streetViewControl: false,
        keyboardShortcuts: false
    };
    var map = new google.maps.Map(document.getElementById(map_id), myOptions);
}

function load_map_and_street_view_from_address(address) {
  // Check if GPS has been locally cached.
  var geocoder = new google.maps.Geocoder();
  var directionsService = new google.maps.DirectionsService();
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {

      var gps = results[0].geometry.location;
      // create_map_and_streetview(gps.lat(), gps.lng(), 'map_canvas', 'pano');

      var request = {
        origin: address,
        destination: address,
        travelMode: google.maps.TravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          var cameraLatLng = response.routes[0].legs[0].steps[0].start_location;
          create_map_and_streetview(gps, cameraLatLng, 'map_canvas', 'pano');
        }
      });
    }
  });
}

function create_map_and_streetview(addLatLng, panoLatLng, map_id, street_view_id) {

  var panorama = new google.maps.StreetViewPanorama(document.getElementById(street_view_id));
  var service = new google.maps.StreetViewService();
  service.getPanoramaByLocation(panoLatLng, 50, function(panoData, status) {
    if (status != google.maps.StreetViewStatus.OK) {
      $('#pano').html('No StreetView Picture Available').attr('style', 'text-align:center;font-weight:bold').show();
      return;
    }
    var marker = new google.maps.Marker({
      map: map,
      icon: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle_blue.png",
      position: panoData.location.latLng
    });
    var angle = google.maps.geometry.spherical.computeHeading(panoData.location.latLng, addLatLng);
    document.getElementById('angle').innerHTML = angle;
    var panoOptions = {
      position: panoLatLng,
      pov: {
        heading: angle,
        pitch: 0,
        zoom: 1
      },
      enableCloseButton: false,
      visible: true
    };

    panorama.setOptions(panoOptions);
  });

  var myOptions = {
    zoom: 16,
    center: addLatLng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    backgroundColor: 'transparent',
    streetViewControl: false,
    keyboardShortcuts: false
  };
  var map = new google.maps.Map(document.getElementById(map_id), myOptions);
  var marker = new google.maps.Marker({
    map: map,
    animation: google.maps.Animation.DROP,
    icon: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
    position: addLatLng
  });
}

$(document).ready(function() {
  console.log('ready');
  load_map_and_street_view_from_address("2131 S SAN ANTONIO AVE, ONTARIO, CA 91762");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?ext=.js"></script>
<div id="angle"></div>
<div id="map_canvas" style="width: 400px; height: 400px; border: 1px solid orange; display: inline-block;"></div>
<div id="pano" style="width: 400px; height: 400px; border: 1px solid green; display: inline-block;"></div>