gpt4 book ai didi

javascript - 谷歌地图 : Uncaught ReferenceError: google is not defined

转载 作者:行者123 更新时间:2023-12-01 01:12:01 25 4
gpt4 key购买 nike

当我尝试使用 map 加载 HTML 页面时收到此错误:

Uncaught ReferenceError: google is not defined

我以为这是我加载 Google map API 的顺序,但我一开始就有了。

我的 HTML 看起来像这样:

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJT$
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap"
async defer></script>
<script src="mapCode.js"></script>


<title>Adventure Map</title>

</head>
<body>
<select id="type" onchange="filterMarkers(this.value);">
<option value="">All Markers</option>
<option value="backpacking">Backpacking</option>
<option value="hiking">Hiking</option>
<option value="offRoad">Off Road</option>
</select>

<div id="map-canvas" style="width: 100%; height: 600px"></div>

</body>

我的html代码调用的mapCode.js看起来像这样。我的顺序是否错误,是不是在加载 map 之前没有及时加载 google API?:

var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
content: ''
});
var markerCluster;

// Our markers
markers1 = [
['0', 'Tanner Trail Grand Canyon', 36.0326, -111.8525, 'backpacking'],
['1', 'Yosemite Snow Shoe Badger Pass', 37.6648, -119.6634, 'backpacking'],
['2', 'Kayak Camping Catalina Island', 33.3504, -118.3282, 'backpacking'],
['3', 'Mini Trans Catalina Trail', 33.3403, -118.3262, 'backpacking']
];

/**
* Function to init map
*/

function initialize() {
var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
var mapOptions = {
zoom: 3,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN
};

map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
for (i = 0; i < markers1.length; i++) {
addMarker(markers1[i]);
}
markerCluster = new MarkerClusterer(map, gmarkers1, {
imagePath: 'https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m'
});
}

/**
* Function to add marker to map
*/

function addMarker(marker) {
var category = marker[4];
var title = marker[1];
var pos = new google.maps.LatLng(marker[2], marker[3]);
var content = marker[1];

marker1 = new google.maps.Marker({
title: title,
position: pos,
category: category,
map: map
});

gmarkers1.push(marker1);

// Marker click listener
google.maps.event.addListener(marker1, 'click', (function(marker1, content) {
return function() {
console.log('Gmarker 1 gets pushed');
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
//map.setZoom(15);
}
})(marker1, content));
}

/**
* Function to filter markers by category
*/

filterMarkers = function(category) {
var newmarkers = [];
for (i = 0; i < markers1.length; i++) {
marker = gmarkers1[i];
// If is same category or category not picked
if (marker.category == category || category.length === 0) {
marker.setVisible(true);
newmarkers.push(marker);
}
// Categories don't match
else {
marker.setVisible(false);
}
}
markerCluster.clearMarkers();
markerCluster.addMarkers(newmarkers);
}
google.maps.event.addDomListener(window, "load", initialize);

最佳答案

我认为一个错误是你需要改变

<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initMap" async defer></script> 

进入

<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&callback=initialize" async defer></script> 

你没有 initMap 函数,但我假设你想调用initialize()。

关于javascript - 谷歌地图 : Uncaught ReferenceError: google is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com