简而言之,我试图完成的工作:
- 地图上的单个信息窗口,因为有很多标记
- 附加到标记的 InfoWindow 内容
- InfoWindow 在标记单击时打开 => 附加属性中的内容
- InfoWindow 包含 2 个按钮 => 设置开始,设置目标
- 如果设置了起点和目标,则计算路线
我尝试了很多,很难将处理程序附加到 InfoWindow 内的按钮。我发现的最好的提示是这个:Adding event to element inside Google Maps API InfoWindow
到目前为止我试过这个:
<script>
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(50.903033, 11.359863),
zoom: 6,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
zoomControl: true,
scaleControl: true,
overviewMapControl: true
};
var map = new google.maps.Map(document.getElementById('limousine-map'),
mapOptions);
var directionsService = new google.maps.DirectionsService();
var directionDisplay = new google.maps.DirectionsRenderer();
directionDisplay.setMap(map);
directionDisplay.setPanel(document.getElementById('route-details'));
var infoWindow = new google.maps.InfoWindow();
var endPoint = false;
var startPoint = false;
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(50.903033, 11.359863),
title: 'Test1'
});
marker.infoWindowContent = [
'<div id="infoWindow"><strong>Test</strong>',
'<button class="button" id="selectStart">Start</button>'+
'<button class="button" id="selectTarget" style="float: right;">Target</button></div>'
].join('<br>');
google.maps.event.addListener(marker,'click',function(){
infoWindow.close();
infoWindow = new google.maps.InfoWindow({
content: this.infoWindowContent
});
infoWindow.open(map,this);
clickedLocation = this;
google.maps.event.addListener(infoWindow, 'domready', function() {
$('#infoWindow button').on('click',function(){
console.log(clickedLocation);
if($(this).attr('id') == 'selectStart'){
startPoint = clickedLocation;
}
if($(this).attr('id') == 'selectTarget'){
endPoint = clickedLocation;
}
recalcRoute();
});
});
});
var marker2 = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(52.903033, 12.359863),
title: 'Test2'
});
marker2.infoWindowContent = [
'<div id="infoWindow"><strong>Test2</strong>',
'<button class="button" id="selectStart">Start</button>'+
'<button class="button" id="selectTarget" style="float: right;">Target</button></div>'
].join('<br>');
google.maps.event.addListener(marker2,'click',function(){
infoWindow.close();
infoWindow = new google.maps.InfoWindow({
content: this.infoWindowContent
});
infoWindow.open(map,this);
clickedLocation = this;
});
//Route painting
function recalcRoute(){
if(startPoint != false && endPoint != false){
var request = {
origin: startPoint,
destination: endPoint,
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
directionsService.route(request, function(response, status){
if(status == google.maps.DirectionsStatus.OK){
$('#route-details').parent().fadeIn();
directionDisplay.setDirections(response);
}
});
}else{
directionDisplay.setDirections(null);
}
}
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
我让处理程序工作,但是路线计算不起作用。我收到一个错误,例如
Uncaught Error: Invalid value for property <origin>: [object Object]
thrown at line: directionsService.route(request, function(response, status){
如何将事件绑定到 InfoWindow 内的按钮并仍然调用 recalcRoute?我也尝试onclick
在按钮上进行设置,但对 recalcRoute 的引用是未知的。
为什么$('#infoWindow button').on('click',function(){ console.log('test'); });
不工作?