javascript - 标记不会出现在 GeoChart 上,但 map 显示得很好

标签 javascript arrays google-maps google-maps-api-3 markers

我在将地理图表标记加载到 map 上时遇到问题。 Google 提供的脚本在我的网站上呈现空白 map ,但标记和 map 在 jsfiddle 上显示应有的样子。任何帮助将不胜感激。

代码如下:

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
<script type="text/javascript" src="https://www.google.com/jsapi"></script>

<script type="text/javascript">

google.charts.load('current', {'packages': ['geochart']});
google.charts.setOnLoadCallback(drawMarkersMap);

  function drawMarkersMap() {
  var data = google.visualization.arrayToDataTable([
    ['City', 'Collaborator'],
    ['Detroit', "text here"],
    ['Cleveland', "text here"],
    ['Joondalup', "text here"],
    ['Wanaka', "text here"]
    ['Liverpool', "text here"],
    ['Styria', "text here"],
    ['Edwardsville', "text here"],
    ['Austin', "text here"],
    ['Houston', "text here"],
    ['Stockholm', "text here"],
    ]);

  var options = {
    region: 'world',
    displayMode: 'markers',
    backgroundColor: '#252426',
    colorAxis: {colors: ['blue']},
    magnifyingGlass: {enable: false},
    defaultColor: '#f1910e',
    enableRegionInteractivity: 'true',
     };

  var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
  chart.draw(data, options);
};
</script>

最佳答案

我明白了!显然,谷歌现在需要 API key 才能在地理图表上显示标记。这是修改后的功能脚本。 第二行是唯一的变化。

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script> 
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?key=YOUR_API_KEY_GOES_HERE" async="" defer="defer"></script>

<script type="text/javascript">

google.charts.load('current', {'packages': ['geochart']});
google.charts.setOnLoadCallback(drawMarkersMap);

function drawMarkersMap() {
var data = google.visualization.arrayToDataTable([
['City', 'Collaborator'],
['Detroit', "text here"],
['Cleveland', "text here"],
['Joondalup', "text here"],
['Wanaka', "text here"]
['Liverpool', "text here"],
['Styria', "text here"],
['Edwardsville', "text here"],
['Austin', "text here"],
['Houston', "text here"],
['Stockholm', "text here"],
]);

var options = {
region: 'world',
displayMode: 'markers',
backgroundColor: '#252426',
colorAxis: {colors: ['blue']},
magnifyingGlass: {enable: false},
defaultColor: '#f1910e',
enableRegionInteractivity: 'true',
};

var chart = new google.visualization.GeoChart(document.getElementById('chart_div'));
chart.draw(data, options);
};
</script>

只要您的网站没有收到太多流量,API key 就可以通过 Google 免费获得。您需要注册一个“浏览器 key ”。这是进行设置的链接: https://developers.google.com/maps/documentation/javascript/get-api-key

关于javascript - 标记不会出现在 GeoChart 上,但 map 显示得很好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38212575/

相关文章:

javascript - 尝试在网站上抓取由谷歌地图API生成的动态数据,但正常抓取返回空白

javascript - Cypress 变量和期望值

javascript - 数据库插入后,Angularjs 通过 $http.get 服务刷新 View

javascript - 无法从上到下交换 div。否则有效

c - 将数组 segmentation 为较小的数组不会返回正确的结果

google-maps - 间歇性谷歌地图 api 授权错误

javascript - 传入指令而不将其添加到范围中

python - 查找列表中第 n 个项目的索引

arrays - 在 VBA 中隐藏连续值

javascript - 从数组中在谷歌地图上放置多个标记