从HTML按钮中的javascript函数加载google地图方向层时出现问题
我制作了一个页面,上面有谷歌地图,还有一个无序的地方列表,这些地方是从附近的搜索请求中获取的。99%的工作正常,但我一辈子都无法在列表项的innerHTML中获得工作链接从HTML按钮中的javascript函数加载google地图方向层时出现问题,javascript,html,Javascript,Html,我制作了一个页面,上面有谷歌地图,还有一个无序的地方列表,这些地方是从附近的搜索请求中获取的。99%的工作正常,但我一辈子都无法在列表项的innerHTML中获得工作链接 function callback(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { document.getElementById("ulPlaces").in
function callback(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
document.getElementById("ulPlaces").innerHTML = "";
for (var i = 0; i < results.length; i++) {
var placeRequest = {
placeId: results[i].place_id
};
service.getDetails(placeRequest, function(resultDetails, status) {
if (status == 'OK') {
placeWebsite = resultDetails.website;
placePhoneNumber = resultDetails.formatted_phone_number;
}
});
var node = document.createElement("li");
node.setAttribute("class", "liPlaces");
node.setAttribute("id", results[i].place_id);
node.innerHTML = '<img class="imgPlaces" src="' + results[i].icon + '" alt="" /><h4 class="h4Places">' + results[i].name + '</h4><p>Open: ' + results[i].opening_hours.open_now + ' | Rating: ' + results[i].rating + '</p><p><input type="button" onclick="loadDirections(' + results[i].geometry.location + ')" value="Directions" /> | <input type="button" onclick="window.location.href=tel:' + placePhoneNumber + ';" value="Telephone" /> | <input type="button" onclick="window.location.href=' + placeWebsite + ';" value="Website" /></p>';
document.getElementById("ulPlaces").appendChild(node);
/*
infoWindow.setPosition(results[i].geometry.location);
infoWindow.setContent(results[i].name);
infoWindow.open(map, marker);
*/
var image = {
url: results[i].icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
};
var marker = new google.maps.Marker({
map: map,
icon: image,
title: results[i].name,
position: results[i].geometry.location
});
marker.addListener('click', (function(marker, i) {
return function() {
//if( prev_infowindow ) {
// prev_infowindow.close();
//}
window.scrollTo(0, document.getElementById(results[i].place_id).offsetTop)
//loadDirections(results[i].geometry.location)
//prev_infowindow = infoWindow;
}
})(marker, i));
}
}
}
function loadDirections(end){
var path = {
origin: pos,
destination: end,
travelMode: 'WALKING'
};
directionsService.route(path, function(resultDirections, statusRoute) {
if (statusRoute == 'OK') {
directionsRenderer.setDirections(resultDirections);
}
});
}
函数回调(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
document.getElementById(“ulPlaces”).innerHTML=“”;
对于(var i=0;i|';
document.getElementById(“ulPlaces”).appendChild(节点);
/*
infoWindow.setPosition(结果[i].geometry.location);
infoWindow.setContent(结果[i].name);
信息窗口。打开(地图、标记);
*/
变量图像={
url:results[i]。图标,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
var marker=new google.maps.marker({
地图:地图,
图标:图像,
标题:结果[i]。名称,
位置:结果[i].geometry.location
});
addListener('click',(函数(marker,i){
返回函数(){
//如果(上一个信息窗口){
//上一个信息窗口。关闭();
//}
scrollTo(0,document.getElementById(结果[i].place\u id.offsetTop)
//加载方向(结果[i]。几何体。位置)
//prev_infowindow=infowindow;
}
})(marker,i));
}
}
}
函数加载方向(结束){
变量路径={
产地:pos,,
目的地:完,
travelMode:“步行”
};
路线(路径、功能(结果方向、状态路线){
如果(statusRoute=='OK'){
directionsRenderer.setDirections(结果方向);
}
});
}
回调函数是搜索查询返回。这里有一个for循环来遍历它们,还有一个请求来获取更具体的细节(网站/电话号码)。然后,它创建“var节点”,并添加class和id属性以及一些innerHTML
如果我将代码从innerHTML中取出,它就会工作。标记上附加了一个单击功能,loadDirections被注释掉。该调用有效,只是innerHTML中的调用无效
打电话和导航到place网站的按钮也可能是死按钮
有什么想法吗?
谢谢。是
onclick=“loadDirections('+results[i].geometry.location+')”
您所指的代码不起作用?如果使用浏览器开发工具检查该元素,onclick=”“
中的值是什么样的?类似于onclick=“loadDirections([object object])”
也许?您好,太好了,谢谢您的回复。是的,就是那个。它返回一个带有lat/long的JSON对象作为Google地图标记。返回的对象返回正确,下面几行注释掉的函数调用工作正常。如果我在“node.innerHTML=”“”之前和之后都调用该函数,它工作正常,只有当我将该函数移动到onclick事件时,它才会失败!如果使用浏览器开发工具检查该元素,onclick=”“中的值是什么样的?