【发布时间】:2019-03-31 17:33:42
【问题描述】:
在升级我的 Route Tracker 应用的过程中,我遇到了与 Google Maps API 未加载相关的问题,导致出现错误“Uncaught ReferenceError: google is not defined”。
我将 google maps API URL 包括在内,如下所示:
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
并且错误是由Javascript中的以下语句生成的:
coords = new google.maps.LatLng(myLat, myLong);
这个应用程序以前运行得很好,但不幸的是不再是因为我怀疑谷歌引入了重大变化。我已经查看了与相同错误消息对应的答案,但到目前为止无济于事。
javascript代码sn-ps如下所示:
function ShowLocation(position)
{
var coords;
var markerOptions = {
map: geolocationClass.map,
position: coords
};
// Fetch coordinates
var myLat = position.coords.latitude;
var myLong = position.coords.longitude;
// Store previous coordinates
coordinatesClass.lat[distanceTimeClass.counter] = myLat;
coordinatesClass.lng[distanceTimeClass.counter] = myLong;
// Google API-ready latitude and longitude
coords = new google.maps.LatLng(myLat, myLong); <--- Error here
.............
.............
}
函数 ShowLocation 调用如下:
function GetLocationUpdate()
{
..............
if (navigator.geolocation)
{
geolocationClass.geoLoc = navigator.geolocation;
geolocationClass.watchID =
geolocationClass.geoLoc.watchPosition(ShowLocation,
ErrorHandler, options);
...........
}
............
}
【问题讨论】:
-
地理定位很可能在 API 加载之前正在运行/返回。您可以尝试使用LatLngLiteral(不需要加载 API)。请提供一个 minimal reproducible example 来证明您的问题。
标签: javascript google-maps-api-3