我目前正在为网站开发一小部分功能。基本上我想在页面上显示港口 map ,并允许用户在其下方输入他们的地址以便找到方向。一旦他们点击“获取路线”链接,就会打开一个精美的框,其中的内容是包含 Google 路线面板的 div。
这是我遇到的问题。 - calcRoute() 函数在链接的 onClick 时调用。因此该 div 之前尚未填充。结果?第一次点击 fancybox 会生成空的。第二次它就会像预期的那样生成。我想出的一种解决方案是手动设置 div 的大小。然而,这会产生滚动和样式问题,我宁愿避免。
这是我的代码(请注意,这是我创建的一个小页面,用于在移植到我们的实际站点之前测试我的代码):
<script type="text/javascript">
var map;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var manhattanPort = new google.maps.LatLng(40.767899, -73.995825);
var myHouse = new google.maps.LatLng(40.419866, -74.152263);
var geoCoder;
function initialize() {
geoCoder = new google.maps.Geocoder();
directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions = {
center: manhattanPort,
zoom:14,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}
function calcRoute(){
var address = document.getElementById("address").value;
var end = manhattanPort;
var request = {
origin: address,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
geoCoder.geocode( {'address':address}, function(results,status){
if(status == google.maps.GeocoderStatus.OK){
map.setCenter(results[0].geometry.location);
} else{
alert("Geocode was not succesful for the following reason: " + status);
}
});
directionsService.route(request, function(result, status){
if( status == google.maps.DirectionsStatus.OK){
directionsDisplay.setDirections(result);
}
});
}
function doClear(theText){
if (theText.value == theText.defaultValue){
theText.value = ""
}
}
</script>
</head>
<body onload="initialize()">
<table align="right">
<tr>
<td align="right" width="330" height="380">
<div id="map_canvas" style="width:100%; height:100%"></div>
</td>
</t
</table>
<table align="center">
<tr>
<td align="center">
<div>
<input name="start_address" id="address" type="text" value="Enter An Address To Get Directions" onFocus="doClear(this)" size="100"/>
<!-- <input type="button" value="Get Directions" onClick="calcRoute();"/> -->
<a class="fancybox" href="#directionsPanel" onClick="calcRoute();">Get Directions</a>
</div>
</td>
</tr>
<tr>
<td align="center">
<div id="directionsPanel"></div>
</td>
</tr>
</table>
</body>
</html>
现在显然我缺少 fancybox 本身的脚本。让我知道提供这些是否有帮助。
感谢您的帮助!
最佳答案
调用$.fancybox.update()
在directionsService.route()
的回调中
关于javascript - 需要帮助让 Google map DirectionPanel 在 FancyBox 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12408168/