【问题标题】:Google Maps API Street view displaying greyGoogle Maps API 街景显示为灰色
【发布时间】:2015-07-30 05:44:10
【问题描述】:

我正在尝试通过 google maps api 街景显示企业内部。我首先检索places_id 并使用它来检索可能可用的任何街景全景图。这在本周早些时候起作用并且已经停止。不过,我不知道为什么。显示交互式地图的 div 显示为灰色,并带有一些 google 工件,并且控制台记录了一个错误,即 google 生成的 url 返回了 404。有人遇到过这种情况吗?

下面的代码也在我的JS Fiddle

var map;
var panorama;

$('#thebutton').click(function () {

    map = new google.maps.Map(document.getElementById('map-canvas'));
    var infowindow = new google.maps.InfoWindow();
    var place_id = 'ChIJ-_RcvsBV2YARhkUbjSD3Vi4';
    var placeservice = new google.maps.places.PlacesService(map);
    var placesRequest = {
        placeId: place_id
    };

    placeservice.getDetails(placesRequest, function (results, status) {

        if (status == google.maps.places.PlacesServiceStatus.OK) {
            var marker = new google.maps.Marker({
                map: map,
                position: results.geometry.location
            });
            google.maps.event.addListener(marker, 'click', function () {
                infowindow.setContent(results.name);
                infowindow.open(map, this);
            });
            var brewerystreetview = new google.maps.LatLng(results.geometry.location.A, results.geometry.location.F);

            var sv = new google.maps.StreetViewService();
            panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), {
                center: new google.maps.LatLng(results.geometry.location.lat(), results.geometry.location.lng()),
                zoom: 0
            });
            sv.getPanorama({
                location: brewerystreetview,
                radius: 0
            }, 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: 0,
            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: 0,
                pitch: 0
            });
            panorama.setVisible(true);
            panorama.show();
        });
    }
}

【问题讨论】:

  • 只是一个旁注,因为它似乎不是主要问题:var brewerystreetview = new google.maps.LatLng(results.geometry.location.A, results.geometry.location.F); A & F 是无证属性。你应该使用lat()lng() 函数,所以new google.maps.LatLng(results.geometry.location.lat(), results.geometry.location.lng()); (你在下面几行......)
  • 嘿@MrUpsidown,感谢您的建议。我做了那个小改动,全景开始按预期填充! 除了在我的 android 上。呃不知道为什么这在一两天前对未记录的属性有效。但是,我会接受的!谢谢!
  • 嗯,这些属性可能会发生变化,特别是如果您不强制使用 API 版本。但是这里有一些奇怪的东西,因为我在一小时前在你的小提琴中改变了它,但它没有用。现在它可以工作了。错误是getPanorama 不是函数(?!)。我什至尝试过使用较旧的 API 版本,但问题是相同的。所以我认为 Google 有问题?
  • 这是可能的,但我通常会先怀疑我的代码,然后再指向其他任何内容。 :) 这个问题发生在我一天的大部分时间里(而昨天它起作用了)。

标签: javascript jquery css google-maps google-maps-api-3


【解决方案1】:
  1. 仅供参考,.getPanorama 函数是 v3.21 (v.exp) 中的新功能,在此之前(v3.20 及更早版本)它是 .getPanoramaById 和 .getPanoramaByLocation。我怀疑你下面的 API 发生了变化(默认为你提供的实验版本从 v3.20 更改为 v3.21)

  2. 您应该永远使用 Google Maps Javascript API 的未记录属性(results.geometry.location.A、results.geometry.location.F),这些属性可以而且确实会随着每个API 的发布。在您使用这些的大多数地方,您可以只使用返回的google.maps.LatLng,而不是创建一个新的。

  3. StreetViewPanorama 没有center 选项,应该是position

panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), {
    center: results.geometry.location,
    zoom: 0
});     

应该是:

panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), {
    position: results.geometry.location,
    zoom: 0
});

updated fiddle

代码 sn-p:

var map;
var panorama;

$('#thebutton').click(function() {
  map = new google.maps.Map(document.getElementById('map-canvas'));
  var infowindow = new google.maps.InfoWindow();
  var place_id = 'ChIJ-_RcvsBV2YARhkUbjSD3Vi4';
  var placeservice = new google.maps.places.PlacesService(map);
  var placesRequest = {
    placeId: place_id
  };
  placeservice.getDetails(placesRequest, function(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
      var marker = new google.maps.Marker({
        map: map,
        position: results.geometry.location
      });
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(results.name);
        infowindow.open(map, this);
      });
      var brewerystreetview = results.geometry.location;

      var sv = new google.maps.StreetViewService();
      panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), {
        position: results.geometry.location,
        zoom: 0
      });
      sv.getPanorama({
        location: brewerystreetview,
        radius: 0
      }, 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: 0,
      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: 0,
        pitch: 0
      });
      panorama.setVisible(true);
      panorama.show();
    });
  }
}
 div#pano {
   width: 100%;
   height: 400px;
   float: left;
 }
 div#map-canvas {
   width: 100%;
   height: 25%;
   float: left;
 }
<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?libraries=places"></script>
<div class="container" id="infocontainer" style=""><a href="#" class="btn btn-primary" id="thebutton" onclick="return false;">Click here</a>
</div>
<div id="pano"></div>
<div id="map-canvas"></div>

【讨论】:

  • 感谢@geocodezip!我试图支持您的回答,但我的声誉仍然太低。 :(
  • 痛苦的是,最近似乎又发生了另一个变化,。即使是更新的小提琴也会出现灰屏 atm
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多