加载谷歌地图onclick事件

时间:2015-12-03 06:43:50

标签: jquery google-maps 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']
        ]);
	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);
      }
}

但这对我不起作用

2 个答案:

答案 0 :(得分: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);
      }
}

答案 1 :(得分: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']
        ]);
	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);
      }
function displayMapAt_map2()
{
  drawChart();
}