我尝试在折线地图制作者上添加标题,但对我而言不起作用。我不明白发生了什么问题或为什么它无法正常工作。 我尝试添加标题或说明,但无法正常工作。 它不会给我控制台错误,但是如果单击它,标题不会出现。
function initialize() {
var map = new google.maps.Map(document.getElementById('map-canvas'), {
center: { lat: 42.9994443, lng: -0.0107599},
zoom: 12
});
var fromMarker = new google.maps.Marker({
map: map,
position: { lat: 43.1002647, lng: -0.0423151 },
title: 'Hello World!'
});
var toMarker = new google.maps.Marker({
map: map,
position: { lat: 42.8906, lng: -0.115006 },
title: 'Hello World!'
});
var ds = new google.maps.DirectionsService();
ds.route({
origin: fromMarker.getPosition(),
destination: toMarker.getPosition(),
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
}, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
console.log(result);
new google.maps.Polyline({
map: map,
path: result.routes[0].overview_path,
strokeOpacity: 1.0,
strokeWeight: 2,
geodesic: true,
icons: [{
icon: {path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW},
offset: '100%',
repeat: '40px'
}]
});
var fullPath = [];
result.routes[0].legs.forEach(function (leg) {
leg.steps.forEach(function (step) {
fullPath = fullPath.concat(step.path);
new google.maps.Polyline({
map: map,
path: step.path,
strokeColor: "red",
strokeWeight: 1
});
});
});
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
标记标题显示在悬停上。如果要在单击时显示信息窗口,则应添加如下内容:
var infowindow = new google.maps.InfoWindow({
content: "<span>Your title</span>"
});
google.maps.event.addListener(fromMarker, 'click', function() {
infowindow.open(map, fromMarker);
});
有关信息窗口的更多信息:https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple