【发布时间】:2020-05-29 15:36:29
【问题描述】:
我正在尝试实现一个谷歌地图,用户可以在其中找到搜索其选择的地方。地图显示得很好,搜索框也可以正常工作。唯一的问题是地图标记没有显示出来。我有以下脚本。似乎从未使用过 icon var,但我不知道为什么。谁能帮帮我?
<script>
function initAutocomplete() {
var map = new google.maps.Map(document.getElementById('map'), {
center: { lat: 55.86515, lng: -4.25763 },
zoom: 13,
mapTypeId: 'roadmap'
});
var input = document.getElementById('pac-input');
var 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());
});
var markers = [];
searchBox.addListener('places_changed', function () {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
markers.forEach(function (marker) {
marker.setMap(null);
});
markers = [];
var bounds = new google.maps.LatLngBounds();
places.forEach(function (place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
var 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: "~/flag.jpg",
title: place.name,
position: place.geometry.location,
animation: google.maps.Animation.BOUNCE
}));
if (place.geometry.viewport) {
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
【问题讨论】:
-
图标
"~/flag.jpg"不可用。使用完全限定的 URL。您可以通过注释掉该行 (icon: "~/flag.jpg",) (fiddle) 来验证代码是否正常运行
标签: c# asp.net google-maps google-maps-api-3