我有js脚本文件。我在哪里使用谷歌热图
这是js文件的代码
$(document).ready(function () {
//map initialization
function initMap() {
map = new google.maps.Map(document.getElementById('map'),
{
zoom: 5,
center: { lat: 51.0742853, lng: 11.0294547 },
mapTypeId: 'roadmap'
});
var centerControlDiv = document.createElement('div');
var centerControlDiv2 = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);
var centerControl2 = new CenterControl1(centerControlDiv, map);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
getDriving();
}
});
在视图中我有脚本
script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCea6mL2cqwVid2ESIjuJ0C31RbNVQNPY0&libraries=visualization&callback=initMap">
当我运行项目时出现initMap is not a function
错误。我该如何解决它。
删除callback=initMap
无法解决问题
以下是脚本的定义方式
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCea6mL2cqwVid2ESIjuJ0C31RbNVQNPY0&libraries=visualization&callback=initMap">
</script>
<script src="~/Scripts/DataTables-1.10.2/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/plug-ins/a5734b29083/integration/bootstrap/3/dataTables.bootstrap.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/plug-ins/a5734b29083/integration/bootstrap/3/dataTables.bootstrap.css" />
<script src="~/Scripts/jquery-ui.min.js"></script>
<script src="~/Scripts/Datepicker-Languages/datepicker-languages.js"></script>
<link href="~/Content/Styles/jquery-ui.css" rel="stylesheet" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.3/moment.min.js"></script>
<script src="~/Scripts/jQuery-ui-extensions/weekMonthDatepicker.js?v=1.0.0.0"></script>
<script src="~/Scripts/Reports/Heatmap.js"></script>
此行中的InitMap <script src="~/Scripts/Reports/Heatmap.js"></script>
答案 0 :(得分:1)
删除async,它告诉浏览器独立于正在加载的其余代码解析代码,并且只依赖于defer,它告诉浏览器等到文档加载直到执行它为止。
<script defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCea6mL2cqwVid2ESIjuJ0C31RbNVQNPY0&libraries=visualization&callback=initMap">
或者将文件中脚本的顺序更改为:
<script src="~/Scripts/Reports/Heatmap.js"></script>
<script async
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCea6mL2cqwVid2ESIjuJ0C31RbNVQNPY0&libraries=visualization&callback=initMap">
</script>
您也无需等待文档加载(也不想)定义您的函数,因此请删除第一行和最后一行。
//map initialization
function initMap() {
map = new google.maps.Map(document.getElementById('map'),
{
zoom: 5,
center: { lat: 51.0742853, lng: 11.0294547 },
mapTypeId: 'roadmap'
});
var centerControlDiv = document.createElement('div');
var centerControlDiv2 = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);
var centerControl2 = new CenterControl1(centerControlDiv, map);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
getDriving();
}