元素总而言之一句话,我尽量做到:谷歌地图第3处理点击内部信息窗口
- 在地图上的单一信息窗口,因为许多指标
- 信息窗口的内容附加到标记
- 信息窗口打开的上的标记的点击=>从附着属性内容
- 信息窗口包含2个按钮=>组开始,设置目标
- 如果开始和目标设定,计算路径
我想了很多,这是相当困难的一个处理程序附加到按钮的信息窗口内。我找到了最好的提示是这样的一个: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){
错误我怎么能一个事件绑定到信息窗口内的按钮,还自称recalcRoute? 我也尝试在按钮上设置onclick
,但对recalcRoute的引用未知。
为什么$('#infoWindow button').on('click',function(){ console.log('test'); });
无法正常工作?
你说的是“地图上的单个InfoWindow”,但代码另有说明。每次单击“标记”时都会创建一个新的infoWinow。 – 2013-03-24 23:17:28