2
当前,当用户在我们的地图屏幕上单击位置标记时,会调用页面方法从服务器检索附加信息。当页面方法成功时,结果用于定义地图上显示的infoWindow的内容。在页面方法很慢的情况下,我们希望infoWindow立即显示,但带有一个加载指示器。页面方法成功后,infowWindow的内容将被更新。将加载指示器添加到Google地图infoWindow
到目前为止,我的天真方法是最初创建带有加载指示器的infoWindow,并通过调用open(map)显示此初始infoWindow,然后在page方法成功后更新该infoWindow的内容。但是,这种方法不起作用,因为地图画布在页面方法完成之后才会更新(因此不会显示infoWindow的初始版本)。
-----页面代码-----
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.8&client=MY_CLIENT&sensor=false"></script>
<script type="text/javascript">
function initialize_map() {
map = new google.maps.Map(...);
// set remaining map properties...
}
window.onload = function() {
initialize_map();
}
function DrawPoint(loc) {
var marker = GetPointMarker(loc);
// set remaining point marker properties...
marker.setMap(map);
var showPointInfo = function (evt) {
var infoWindow = infowindowList[loc];
if (infoWindow == undefined)
{
GetPointInfoStart(loc);
GetPointInfo(loc);
}
};
google.maps.event.addListener(marker, 'click', showPointInfo);
}
function GetPointInfoStart(loc)
{
var infoWindow = new google.maps.InfoWindow();
var content = // initial content with loading indicator
infoWindow.setContent(content);
// set remaining infoWindow properties...
infoWindow.open(map);
}
function GetPointInfo(loc)
{
// call page method to retrieve data for infoWindow
PageMethods.GetMapPointInfo(..., OnGetPointInfoSuccess, OnFailure);
}
function OnGetPointInfoSuccess(result) {
eval(result);
var infoWindow = infowindowList[loc];
var content = // final content with retrieved data
infoWindow.setContent(content);
}
</script>
-----代码背后-----
protected override void OnInit(EventArgs e)
{
ScriptManager.GetCurrent(this).EnablePageMethods = true;
...
base.OnInit(e);
}
[WebMethod]
public static string GetMapPointInfo(...)
{
// retrieve point information from server...
return jsonString;
}