2015-12-03 102 views
-1

我使用下面的代碼加載谷歌地圖onclick事件

function displayMapAt_map2() 
 
{ 
 
     google.load("visualization", "1", {packages:["map"]}); 
 
     google.setOnLoadCallback(drawChart); 
 
     function drawChart() { 
 
     var data = google.visualization.arrayToDataTable([ 
 
      ['Lat', 'Long', 'Name'], 
 
      [-6.6,106.829635, 'Work'], 
 
      [-6.6,106.859665, 'University'], 
 
      [-6.6,106.849685, 'Airport'], 
 
      [-6.6,106.839685, 'Shopping'] 
 
     ]); 
 
\t var options = { 
 
\t showTip: true, 
 
\t center: location, 
 
\t zoom: 2, 
 
\t mapType: 'normal', 
 
\t enableScrollWheel: true 
 
\t } 
 

 
     var map = new google.visualization.Map(document.getElementById('map-canvas2')); 
 
     map.draw(data,options); 
 
     } 
 
}

但是這不是爲我工作

+2

有控制檯任何錯誤? –

+0

沒有。我只是在點擊add而點燃了這個事件,但頁面被重新加載 –

+0

這個問題已經得到解答。看看這個http://stackoverflow.com/questions/23404763/load-google-map-on-button-click-with-jquery – claudios

回答

0

我得到的回答這一點。它幫助我這樣

 google.load("visualization", "1", {packages:["map"]}); 
 
     google.setOnLoadCallback(drawChart); 
 
     function drawChart() { 
 
     var data = google.visualization.arrayToDataTable([ 
 
      ['Lat', 'Long', 'Name'], 
 
      [-6.6,106.829635, 'Work'], 
 
      [-6.6,106.859665, 'University'], 
 
      [-6.6,106.849685, 'Airport'], 
 
      [-6.6,106.839685, 'Shopping'] 
 
     ]); 
 
\t var options = { 
 
\t showTip: true, 
 
\t center: location, 
 
\t zoom: 2, 
 
\t mapType: 'normal', 
 
\t enableScrollWheel: true 
 
\t } 
 

 
     var map = new google.visualization.Map(document.getElementById('map-canvas2')); 
 
     map.draw(data,options); 
 
     } 
 
function displayMapAt_map2() 
 
{ 
 
    drawChart(); 
 
}

0

都需要這樣的調用,

<a href="#local" onClick="displayMapAt_map2(event)">add</a> 

然後在功能,

function displayMapAt_map2(e) 
{ 
     e.preventDefault(); 
     google.load("visualization", "1", {packages:["map"]}); 
     google.setOnLoadCallback(drawChart); 
     function drawChart() { 
     var data = google.visualization.arrayToDataTable([ 
      ['Lat', 'Long', 'Name'], 
      [-6.6,106.829635, 'Work'], 
      [-6.6,106.859665, 'University'], 
      [-6.6,106.849685, 'Airport'], 
      [-6.6,106.839685, 'Shopping'] 
     ]); 
    var options = { 
     showTip: true, 
     center: location, 
     zoom: 2, 
     mapType: 'normal', 
     enableScrollWheel: true 
    } 

     var map = new google.visualization.Map(document.getElementById('map-canvas2')); 
     map.draw(data,options); 
     } 
} 
+0

它不適合我。它無限地重新加載頁面: –

+0

也向我們展示了html。您是否在您的代碼中引用Google地圖js? –

+0

  • MAP2