2014-01-29 138 views
2

嗨,我有以下代碼,每次我嘗試添加一個按鈕它不起作用。這是我目前的代碼。有人可以告訴我如何添加一個按鈕,單擊時顯示一個警告框。如何添加一個按鈕,谷歌地圖infowindow

 var infowindow = new google.maps.InfoWindow({ 
     content: " " 
     }); 
     google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent('<p>Event Name: '+this.title+'</p>' + 
       '<p>Event Type: '+this.etype+'</p>' + 
       '<p>Cause: '+this.cause+'</p>' + 
       '<p>Date: '+this.date+'</p>' + 
        '<p>Time: '+this.time+'</p>' + 
        '<button onclick="myFunction()"> 'Click me'</button>'); 


     infowindow.open(map, this); 
     }); 
    }); 
+0

如何你想添加一個按鈕?必要時必須小心引號,單引號和轉義引號。 – geocodezip

+0

[InfoWindows中的函數調用](http://stackoverflow.com/questions/21327691/function-calls-within-infowindows) –

+0

il更新代碼並顯示給您。謝謝 – codingman

回答

3

你已經不匹配'

var infowindow = new google.maps.InfoWindow({ 
    content: " " 
    }); 
    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.setContent('<p>Event Name: '+this.title+'</p>' + 
      '<p>Event Type: '+this.etype+'</p>' + 
      '<p>Cause: '+this.cause+'</p>' + 
      '<p>Date: '+this.date+'</p>' + 
       '<p>Time: '+this.time+'</p>' + 
       '<button onclick="myFunction()">Click me</button>'); 


    infowindow.open(map, this); 
    }); 
}); 

代碼片段:

var infowindow; 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var marker = new google.maps.Marker({ 
 
    position: map.getCenter(), 
 
    map: map 
 
    }); 
 

 
    infowindow = new google.maps.InfoWindow({ 
 
    content: " " 
 
    }); 
 

 
    google.maps.event.addListener(marker, 'click', function() { 
 
    infowindow.setContent('<p>Event Name: ' + this.title + '</p>' + 
 
     '<p>Event Type: ' + this.etype + '</p>' + 
 
     '<p>Cause: ' + this.cause + '</p>' + 
 
     '<p>Date: ' + this.date + '</p>' + 
 
     '<p>Time: ' + this.time + '</p>' + 
 
     '<button onclick="myFunction()">Click me</button>'); 
 
    infowindow.open(map, this); 
 
    }); 
 
    google.maps.event.trigger(marker, 'click'); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize); 
 

 
function myFunction() { 
 
    infowindow.setContent('<div style="background-color: green">' + infowindow.getContent() + "</div>"); 
 
}
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

+0

是不是我的功能超出了範圍?我似乎無法在React中訪問它。 – fungusanthrax

+0

'myFunction'需要位於全局範圍內(因爲它在代碼段中)才能用作HTML中的onclick函數。 – geocodezip