google-maps-api-3 - Google Places 照片 getUrl() 破坏了我的 javascript

标签 google-maps-api-3 google-places-api geturl

我有以下 javascript:

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      place = new Object ({
        name: results[i].name,
        photo: results[i].photos[0].getUrl({'maxWidth': 100, 'maxHeight': 100}),
        loc: results[i].geometry.location,
        rating: results[i].rating,
      })
      places.push(place);
      var marker = new google.maps.Marker({
        map: map,
        position: results[i].geometry.location,
        id: i,
        visible: false,
      });
      markers.push(marker);
    }
  }
  newresult();
}

如果我注释掉以下行,函数 newresult() 将运行:
photo: results[i].photos[0].getUrl({'maxWidth': 100, 'maxHeight': 100}),

但是,在上面的代码中,如果我不将其注释掉,则函数 newresult() 将不会运行。我知道 photo 的 getUrl 函数可以工作,因为它返回一个有效的 url。

谢谢你的帮助。

最佳答案

I know that the getUrl function of photo works as it returns a valid url.



是的,如果 有一张与该地点相关的照片!一个地方没有照片 = 没有 photo results[i] 中的数组目的。然后你的代码中断了。您必须在每次迭代中检查 photo数组在使用之前存在:

place = new Object ({
   name: results[i].name,
   photo: typeof results[i].photos !== 'undefined' 
       ? results[i].photos[0].getUrl({'maxWidth': 100, 'maxHeight': 100})
       : '' //alternative a "nophoto.jpg"
   loc: results[i].geometry.location,
   rating: results[i].rating,
});

这里有一个基于你上面代码的 fiddle -> http://jsfiddle.net/dX9Gu/

关于google-maps-api-3 - Google Places 照片 getUrl() 破坏了我的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22459910/

相关文章:

flash - flash 的 geturl(...) 可以被谷歌抓取吗?

google-maps - 从自动完成 getPlace() 调用返回的 Placeresult 对象缺少几个属性

javascript - 动态创建 Google map LatLng 类

java - 如何检查从 Google Place Search 获得的结果中是否存在 'rating' 字段?

google-maps-api-3 - google place api获取位置的引用值

javascript - 获取传递的 URL 参数并添加到 iframe URL

c++ - 使用 C++ 在 FireFox 中获取事件选项卡 URL

javascript - Google Maps Directions API - 单击按钮时显示备用路线

javascript - 为谷歌地图中的每个标记分配信息窗口

google-places-api - Google Maps Geocoding API - 使用 API key 拒绝请求