【发布时间】: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