繁体   English   中英

我遇到两个错误,有人可以帮助我找到它们吗?

[英]I am getting two errors, Can anyone help me find them?

我遇到两个错误:

TypeError:window.geoTest不是函数

语法错误:丢失; 声明前

这些错误是什么意思? 我看不到缺少分号的地方。 window.geoTest不是函数是什么意思?

 "use strict"; var waitForUser; function geoTest() { waitForUser = setTimeout(fail, 10000); if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(createMap, fail, {timeout: 10000}); } else { fail(); } } function createMap(position) { clearTimeout(waitForUser); var Lat = position.coords.latitude; var Lng = position.coords.longitude; var mapOptions { center: new google.maps.LatLng(Lat, Lng), zoom: 10 }; var map = new google.maps.Map(document.getElementById("map"), mapOptions); } function fail() { document.getElementById("map").innerHTML = "Unable to access your current location"; } 
 <body> <header> <h1> Hands-on Project 10-3 </h1> </header> <article> <div id="cities"> <div id="beijing">Beijing</div> <div id="paris">Paris</div> <div id="rio">Rio de Janeiro</div> </div> <div id="caption"> Your location </div> <div id="map"></div> </article> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&callback=geoTest"></script> <script src="script.js"></script> </body> </html> 

请更正此错误,您错过了“ =“

var mapOptions {
    center: new google.maps.LatLng(Lat, Lng), 
    zoom: 10
};

var mapOptions = {
    center: new google.maps.LatLng(Lat, Lng), 
    zoom: 10
};

语法错误:丢失; 声明前

此错误是由于以下代码段引起的:

var mapOptions {
    center: new google.maps.LatLng(Lat, Lng), 
    zoom: 10
};

因为您缺少赋值运算符,所以=

var mapOptions = {
    center: new google.maps.LatLng(Lat, Lng), 
    zoom: 10
};

即使错误提示您缺少了一个;那本来应该是一个非常简单的错误; 因为该错误通常会提供发生错误的行号。 另一件事是,通常会为任何语法错误引发该错误,并且通常非常接近该语法错误所在的位置。 因此,当我看到这样的错误时,通常会执行任何查找语法错误的操作。


TypeError:window.geoTest不是函数

我想指出的是,我不知道您是否正确使用了google maps api,但我认为此错误是因为您使用的是尚未创建的函数(换句话说,未定义为函数的变量或用作函数的未定义变量)。 因此,在这里您需要转到使用该函数的地方,也就是这里:

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&callback=geoTest"></script>

这是在加载定义函数的script.js之前出现的。 要解决此问题,请交换script标签的位置以获取:

<script src="script.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&callback=geoTest"></script>

现在,将为回调定义函数。


如果要在https://maps.googleapis.com/maps/api/js中调用geoTest() ,则必须在https://maps.googleapis.com/maps/api/js中具有调用它的代码。 不会仅仅因为您将其放在URL中而被调用。 您将需要编写代码以从URL解析它,然后调用它。

或者,您也可以在脚本完成加载后连接通知,然后在https://maps.googleapis之后从https://maps.googleapis.com/maps/api/js外部自己调用geoTest() 。 com / maps / api / js已完全加载。

要检测外部脚本文件何时成功加载,只需自己调用geoTest()

供参考,请点击这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM