我尝试在我的网站上创建一个搜索栏,它将使用两个 Google API。
根据 Google API 文档,我编写了这段代码。
function calcRoute() {
//Setting variables for Google Places API request
var start = 'starting point';
var end = 'destination point';
var service;
var moscow = new google.maps.LatLng(55.749646,37.62368);
var places = [];
var request1 = {
location: moscow,
radius: '50000',
query: start
};
var request2 = {
location: moscow,
radius: '50000',
query: end
};
//Setting variables for Google Directions API
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
//Executing Google Places request
service = new google.maps.places.PlacesService();
service.textSearch(request1, callback1);
service.textSearch(request2, callback2);
function callback1(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
places[0] = results[0].formatted_address;
} else {
document.getElementById("directionsPanel").innerHTML = "<br/><b>Sorry, directions not found.</b><br/><br/>";
return;
}
}
function callback2(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
places[1] = results[1].formatted_address;
//Executing Google Directions request
var request = {
origin:places[0],
destination:places[1],
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var myOptions = {
zoom: 8,
streetViewControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
directionsDisplay = new google.maps.DirectionsRenderer();
map = new google.maps.Map(document.getElementById("map_canvas2"), myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
else
{
document.getElementById("directionsPanel").innerHTML = "<br/><b>Sorry, directions not found.</b><br/><br/>";
};
});
//End of Google Directions request
}
else
{
document.getElementById("directionsPanel").innerHTML = "<br/><b>Sorry, directions not found.</b><br/><br/>";
return;
}
}
//Closing calcRoute() function
}
这是 HTML
<html>
<head>
<link href="../images/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script src="../scripts/map.js" type="text/javascript"></script>
</head>
<body onload="calcRoute();">
<FORM NAME="joe">
<INPUT TYPE="hidden" NAME="burns" />
</FORM>
<div id="external">
<div id="wrap">
<div id="header" align="center"><img src="../images/illustrations/header.gif" alt="Air Moscow" />
</div>
<div style="background:#CCDBE3; margin:0px 5px; float:left; width:736px; margin-bottom:10px;">
<div style="background:#FFF; margin:15px; border:1px #999 solid;">
<div id="directionsPanel"></div>
<div style="float:right; width:370px; height:600px; border:2px solid #306B8E; margin:30px 10px 0px 10px;" id="map_canvas2">
</div>
<div style="clear:both; margin-bottom:50px;"></div>
</div>
</div>
</div>
<div id="footer">
</div>
</div>
</body>
</html>
当我测试它时,chrome js 控制台抛出“Uncaught TypeError: Cannot read property 'innerHTML' of undefined”。但是,两个 div 都已定义,并且它们的 id 中没有拼写错误。
我想我需要一个 API 密钥才能让 Google Places 工作。
除此之外,我还需要什么其他东西才能让这段代码正常工作吗?