簡體   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