我尝试使用谷歌地图实现搜索功能,并遇到了 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">
&#13;
即使我将相同的代码从JSFiddle发布到网站上的代码片段中,它似乎甚至无法加载。
为了避免redundency,我只发布我的html文件,因为CSS和JS与链接的jsfiddle相同。 (Mapper.js - &gt;包含小提琴代码的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&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>
最后,这就是我的页面的样子。请注意google地图与JSFiddle的不同之处。
{{3}}
编辑 - 忘记netbeans向我显示错误
Uncaught TypeError: Cannot read property 'SearchBox' of undefined (15:08:11:023 | error, javascript)
at initialize (public_html/js/Mapper.js:19:43)
答案 0 :(得分:4)
该错误表明Google地图库未加载。
您没有将HTML API中的google api源网址呈现为文本。我认为您的网址应该使用&
而不是&
。
<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>
答案 1 :(得分:0)
问题是边栏上加载了一个外部库,我没有检查。
更改此脚本
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&sensor=false"></script>
到
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>