gpt4 book ai didi

javascript - 无法让 JSFiddle 工作

转载 作者:行者123 更新时间:2023-11-29 18:03:33 31 4
gpt4 key购买 nike

我正在尝试使用谷歌地图实现搜索功能并遇到了 this jsfiddle.

但是,当我尝试用我的网页实现它时,它无法在 JSFiddle 上以相同的方式加载。

我正在使用 Netbeans IDE,并创建了一个 HTML5 元素。这是我的第一个 html 元素,所以我不熟悉 js/html 集成。

根据需要,这里是 JSFiddle 的代码

function initialize() {

var markers = [];
var map = new google.maps.Map(document.getElementById('map-canvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});

var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-33.8902, 151.1759),
new google.maps.LatLng(-33.8474, 151.2631));
map.fitBounds(defaultBounds);

// Create the search box and link it to the UI element.
var input = /** @type {HTMLInputElement} */
(
document.getElementById('pac-input'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

var searchBox = new google.maps.places.SearchBox(
/** @type {HTMLInputElement} */
(input));

// Listen for the event fired when the user selects an item from the
// pick list. Retrieve the matching places for that item.
google.maps.event.addListener(searchBox, 'places_changed', function() {

var places = searchBox.getPlaces();

for (var i = 0, marker; marker = markers[i]; i++) {
marker.setMap(null);
}

markers = [];
var bounds = new google.maps.LatLngBounds();

for (var i = 0, place; place = places[i]; i++) {

// Create a marker for each place.
var marker = new google.maps.Marker({
map: map,
title: place.name,
position: place.geometry.location
});

markers.push(marker);

bounds.extend(place.geometry.location);
}

map.fitBounds(bounds);
});
}

initialize();
#map-canvas {
height: 400px;
width: 600px;
}
.controls {
margin-top: 16px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#pac-input {
background-color: #fff;
padding: 0 11px 0 13px;
width: 400px;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
text-overflow: ellipsis;
}
#pac-input:focus {
border-color: #4d90fe;
margin-left: -1px;
padding-left: 14px;
/* Regular padding-left + 1. */
width: 401px;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
<div id="map-canvas"></div>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">

即使我将 JSFiddle 中的相同代码发布到网站上的代码片段中,它似乎也无法加载。

为避免冗余,我将只发布我的 html 文件,因为 CSS 和 JS 与链接的 jsfiddle 相同。 (Mapper.js -> 包含 fiddle 代码的 js 文件)

<!DOCTYPE html>
<html>

<title>Google Maps with a Table</title>

<link rel="shortcut icon" href="">

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="js/libs/jquery/jquery.js"></script>
<script type="text/javascript" src="js/libs/jqueryui/jquery-ui.js"></script>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&amp;sensor=false"></script>

<link type="text/css" rel="stylesheet" href="css/basecss.css">

<body>

<div id="map-canvas"></div>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<script type="text/javascript" src="js/Mapper.js"></script>


</body>
</html>

最后,这是我的页面的样子。请注意谷歌地图与 JSFiddle map 有何不同。

My page view

编辑 - 忘记了 netbeans 向我显示错误

Uncaught TypeError: Cannot read property 'SearchBox' of undefined (15:08:11:023 | error, javascript)
at initialize (public_html/js/Mapper.js:19:43)

最佳答案

该错误提示未加载 google map 库。

您没有将 google api 源 url 从 html 呈现为文本。我认为您的网址应该使用 & 而不是 &

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>

在 jsFiddle 的外部资源中也缺少 libraries=places

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&libraries=places&sensor=false"></script>

关于javascript - 无法让 JSFiddle 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33133599/

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