google 地图标记图标未显示
google map marker icon not showing
我正在尝试实现一个 google 地图,用户可以在其中搜索自己选择的地点。地图显示得很好,搜索框也能正常工作。唯一的问题是地图标记没有显示。我有以下脚本。似乎从未使用过图标变量,但我不知道为什么。谁能帮帮我?
<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>
你给的图片路径可能不是working.So,请尝试以下验证。 这对我有用。
var markerImage = 'http://www.mapsmarker.com/wp-content/uploads/leaflet-maps-marker-icons/bar_coktail.png';
markers.push(new google.maps.Marker({
map: map,
icon: markerImage,
title: place.name,
position: place.geometry.location,
animation: google.maps.Animation.BOUNCE
}));
可以试试fiddle
我正在尝试实现一个 google 地图,用户可以在其中搜索自己选择的地点。地图显示得很好,搜索框也能正常工作。唯一的问题是地图标记没有显示。我有以下脚本。似乎从未使用过图标变量,但我不知道为什么。谁能帮帮我?
<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>
你给的图片路径可能不是working.So,请尝试以下验证。 这对我有用。
var markerImage = 'http://www.mapsmarker.com/wp-content/uploads/leaflet-maps-marker-icons/bar_coktail.png';
markers.push(new google.maps.Marker({
map: map,
icon: markerImage,
title: place.name,
position: place.geometry.location,
animation: google.maps.Animation.BOUNCE
}));
可以试试fiddle