1

我试图让人们“绕过”传统的谷歌地图,并在输入地址后立即显示街景位置。(我不想与地图街景小人或标记等进行任何交互 - 只需输入地址并显示街景)。

任何帮助将非常感激。

下面是我一直在尝试破解以产生这些结果的代码。但我是新手,有点困惑。

var map;
  var berkeley = new google.maps.LatLng(-29.859515,30.998577);
  var sv = new google.maps.StreetViewService();

  var panorama;

  function initialize() {

    panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'));

    // Set up the map
    var mapOptions = {
      center: berkeley,
      zoom: 16,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      streetViewControl: false
    };
    map = new google.maps.Map(document.getElementById('map-canvas'),
        mapOptions);






         var input = document.getElementById('searchTextField');
    var autocomplete = new google.maps.places.Autocomplete(input);

    autocomplete.bindTo('bounds', map);

    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
      map: map
    });



    google.maps.event.addListener(autocomplete, 'place_changed', function() {

      infowindow.close();
      marker.setVisible(false);
      input.className = '';
      var place = autocomplete.getPlace();
      if (!place.geometry) {
        // Inform the user that the place was not found and return.
        input.className = 'notfound';
        return;
      }

      // If the place has a geometry, then present it on a map.
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
      }




    //edit the doing var <!-- Edit this for auto marking to affect street view --> 

      var markerPanoID = data.location.pano;
        // Set the Pano to use the passed panoID
        panorama.setPano(markerPanoID);
        panorama.setPov({
          heading: 270,
          pitch: 0
        });
        panorama.setVisible(true);




      var address = '';
      if (place.address_components) {
        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
      }

      infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
      infowindow.open(map, marker);
    });



    // getPanoramaByLocation will return the nearest pano when the
    // given radius is 50 meters or less.
    google.maps.event.addListener(map, 'click', function(event) {
        sv.getPanoramaByLocation(event.latLng, 50, processSVData);
    });

  }

  function processSVData(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
      var marker = new google.maps.Marker({
        position: data.location.latLng,
        map: map,
        title: data.location.description
      });

      panorama.setPano(data.location.pano);
      panorama.setPov({
        heading: 270,
        pitch: 0
      });
      panorama.setVisible(true);





      google.maps.event.addListener(marker, 'click', function() {

        var markerPanoID = data.location.pano;
        // Set the Pano to use the passed panoID
        panorama.setPano(markerPanoID);
        panorama.setPov({
          heading: 270,
          pitch: 0
        });
        panorama.setVisible(true);
      });
      } else {
      alert('Street View data not found for this location.');
    }
  }



  google.maps.event.addDomListener(window, 'load', initialize);
4

1 回答 1

1

以下链接可能会回答您的问题。其描述:

  • 如何从地址输入中获取谷歌地图标记和街景。
  • 它还计算视角/偏航角,以便您面对正确的街景角度。

说明:http ://www.jaycodesign.co.nz/js/using-google-maps-to-show-a-streetview-of-a-house-based-on-an-address/

演示:http ://www.jaycodesign.co.nz/wp-content/uploads/demos/StreetView/

更新如上链接已损坏。考虑以下 SO 问题和答案:使用 Google Maps API 根据地址显示房屋的街景

我没有测试过它,但它更流行。

于 2013-03-25T11:06:44.850 回答