【问题标题】:InvalidValueError: setIcon: not a string; and not an instance of PinView; and no url property; and no path propertyInvalidValueError:setIcon:不是字符串;而不是 PinView 的实例;并且没有 url 属性;并且没有路径属性
【发布时间】:2020-02-17 10:02:05
【问题描述】:

我正在开发一款应用,该应用使用 Google Maps/Places API 中的搜索框功能来查找当地城市或地址附近的食品银行。

客户端在测试中看起来不错,但在后台我不断在控制台中收到以下错误消息:“InvalidValueError: setIcon: not a string; and not an instance of PinView; and no url property ; 并且没有路径属性”。它似乎不会影响正在输入/返回的搜索,并返回适合搜索查询的所有内容。我不确定需要做什么才能使错误消失。有什么建议么?非常感谢!

这是我的 JS 代码:

function initAutocomplete() {

    let map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 32.714286, lng: -117.155577},
        scrollwheel: false,
        zoom: 12,
        maptypeId: 'roadmap'
    });

    let input = document.getElementById('pac-input'); 
    let searchBox = new google.maps.places.SearchBox(input);
    map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

    map.addListener('bounds_changed', function() {
      searchBox.setBounds(map.getBounds());
    });

    let markers = [];

    searchBox.addListener('places_changed', function() {
      let places = searchBox.getPlaces();

      if (places.length == 0) {
        return;
      }

      markers.forEach(function(marker) {
        marker.setMap(null);
      });
      markers = [];

      let bounds = new google.maps.LatLngBounds();
      places.forEach(function(place) {
        if (!place.geometry) {
          console.log("Returned place contains no geometry");
          return;
        }

        let icon = {
          url: places.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };

        markers.push(new google.maps.Marker({
          map: map,
          icon: icon,
          title: place.name,
          position: place.geometry.location
        }));

        if (place.geometry.viewport) {
          bounds.union(place.geometry.viewport);
        } else {
          bounds.extend(place.geometry.location);
        }
      });

      map.fitBounds(bounds);
      });
}

【问题讨论】:

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


    【解决方案1】:

    这是无效的:url: places.icon,places 是一个数组,没有 .icon 属性。这意味着urlundefined,给出错误:InvalidValueError: setIcon: not a string; and not an instance of PinView; and no url property; and no path property

    改用url: place.icon,forEach 将数组的每个元素分配给其回调函数中的place 变量)。

    places.forEach(function(place) {
      if (!place.geometry) {
        console.log("Returned place contains no geometry");
        return;
      }
    
      let icon = {
        url: place.icon,
        size: new google.maps.Size(71, 71),
        origin: new google.maps.Point(0, 0),
        anchor: new google.maps.Point(17, 34),
        scaledSize: new google.maps.Size(25, 25)
      };
    
      markers.push(new google.maps.Marker({
        map: map,
        icon: icon,
        title: place.name,
        position: place.geometry.location
      }));
    
      if (place.geometry.viewport) {
        bounds.union(place.geometry.viewport);
      } else {
        bounds.extend(place.geometry.location);
      }
    });
    

    proof of concept fiddle

    代码 sn-p:

    function initAutocomplete() {
    
      let map = new google.maps.Map(document.getElementById('map'), {
        center: {
          lat: 32.714286,
          lng: -117.155577
        },
        scrollwheel: false,
        zoom: 12,
        maptypeId: 'roadmap'
      });
    
      let input = document.getElementById('pac-input');
      let searchBox = new google.maps.places.SearchBox(input);
      map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    
      map.addListener('bounds_changed', function() {
        searchBox.setBounds(map.getBounds());
      });
    
      let markers = [];
    
      searchBox.addListener('places_changed', function() {
        let places = searchBox.getPlaces();
    
        if (places.length == 0) {
          return;
        }
    
        markers.forEach(function(marker) {
          marker.setMap(null);
        });
        markers = [];
    
        let bounds = new google.maps.LatLngBounds();
        places.forEach(function(place) {
          if (!place.geometry) {
            console.log("Returned place contains no geometry");
            return;
          }
    
          let icon = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };
    
          markers.push(new google.maps.Marker({
            map: map,
            icon: icon,
            title: place.name,
            position: place.geometry.location
          }));
    
          if (place.geometry.viewport) {
            bounds.union(place.geometry.viewport);
          } else {
            bounds.extend(place.geometry.location);
          }
        });
    
        map.fitBounds(bounds);
      });
    }
    /* Always set the map height explicitly to define the size of the div
     * element that contains the map. */
    
    #map {
      height: 90%;
    }
    
    
    /* Optional: Makes the sample page fill the window. */
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    
    #description {
      font-family: Roboto;
      font-size: 15px;
      font-weight: 300;
    }
    
    #infowindow-content .title {
      font-weight: bold;
    }
    
    #infowindow-content {
      display: none;
    }
    
    #map #infowindow-content {
      display: inline;
    }
    
    .pac-card {
      margin: 10px 10px 0 0;
      border-radius: 2px 0 0 2px;
      box-sizing: border-box;
      -moz-box-sizing: border-box;
      outline: none;
      box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
      background-color: #fff;
      font-family: Roboto;
    }
    
    #pac-container {
      padding-bottom: 12px;
      margin-right: 12px;
    }
    
    .pac-controls {
      display: inline-block;
      padding: 5px 11px;
    }
    
    .pac-controls label {
      font-family: Roboto;
      font-size: 13px;
      font-weight: 300;
    }
    
    #pac-input {
      background-color: #fff;
      font-family: Roboto;
      font-size: 15px;
      font-weight: 300;
      margin-left: 12px;
      padding: 0 11px 0 13px;
      text-overflow: ellipsis;
      width: 400px;
    }
    
    #pac-input:focus {
      border-color: #4d90fe;
    }
    
    #title {
      color: #fff;
      background-color: #4d90fe;
      font-size: 25px;
      font-weight: 500;
      padding: 6px 12px;
    }
    <div id="pac-container">
      <input id="pac-input" type="text" placeholder="Enter a location" value="starbuck near New York city">
    </div>
    <div id="map"></div>
    <div id="infowindow-content">
      <img src="" width="16" height="16" id="place-icon">
      <span id="place-name" class="title"></span><br>
      <span id="place-address"></span>
    </div>
    <!-- Replace the value of the key parameter with your own API key. -->
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=initAutocomplete" async defer></script>

    【讨论】:

    • 非常感谢!很好的解释。这解决了控制台错误。但是现在标记变成了一个黑盒子,而不是传统的红点。我是否需要在图标对象中设置一个额外的参数来让它显示默认的红色标记?
    • 如果您想要默认的红色标记,请不要使用位置响应中的图标(从marker 构造函数中删除行icon: icon,,以及icon 对象的定义)
    • 我明白了!我已经这样做了,这已经解决了这个次要问题。太感谢了!这是我第一次尝试为一个项目探索/使用 Google Maps API,所以我在旅途中经历了很多尝试/错误。但我真的很感谢你所有的帮助和很好的解释:) 他们真的让我头疼。
    猜你喜欢
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2022-11-10
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多