4

我使用谷歌地图的实现,它在地图上加载由 php 脚本确定的位置的标记,该脚本允许用户根据搜索条件动态过滤兴趣点。这是通过对 php 搜索脚本的 ajax 调用来完成的。这些标记还有一个附加的 infoWindow,它通过访问不同的 php 脚本动态加载有关站点的详细信息。在标记创建期间,我使用以下代码:

// Initialize the info window
var InfoWindow = new google.maps.InfoWindow({ content: 'Display Error: Please Try Again', maxWidth: 400 });

// Lots of code to prep the search filters and now inside an $.ajax().done(function(html) { $('#ajaxStub div.stubCalc').each(function() {  } }) method

var newMarker = new google.maps.Marker({
    position: latlon,
    icon: $(this).find('.iconStub').text(),
    title: $(this).find('.nameStub').text(),
    map: gmap
});
// onClick show the InfoWindow
google.maps.event.addListener(newMarker, 'click', function(e) {
    InfoWindow.content = $('<div class="projectStub" guid=""></div>').attr('guid', guid).get(0);
    InfoWindow.open(gmap, newMarker);
    // loadStub(div, guid, [isToolbox], [callback()]) is defined in CalcInfo.js so that it can also be used by the Calculator Toolbox Page
    loadStub($('div.projectStub[guid="' + guid + '"]').get(), guid, false, function() {
        InfoWindow.setContent(InfoWindow.content);
    });
});

loadStub 方法让您可以看到进行初始 ajax 调用并为文件上传的 ajax 调用做准备(因此此信息窗口的内容非常动态)

function loadStub(project, guid, isToolbox, callback) {
    if (typeof(isToolbox) === 'undefined') isToolbox = false;
    $(project).html('<img src="/Styles/images/ajax-loader.gif"/>');
    $.ajax({
        url: "/ajax.php?ajax=CalcInfo",
        type: "POST",
        data: { guid: guid, isToolbox: isToolbox.toString() },
        cache: false
    }).done(function(html) {
        $(project).html($(html).html());
        $(project).find('#fileInput').change(function() {
            // More code here to prepare for ajax upload
            $.ajax({
                url: "/ajax.php&ajax=Thumbnail&guid=" + guid,
                type: "POST",
                data: formdata
            }).done(function(html) {
                if ($(html).find('#ajaxContent .error').length > 0) {
                    var errorMessage = 'The Server Returned the Following Error(s):\n';
                    $(html).find('#ajaxContent .error').each(function() {
                        errorMessage += $(this).text() + '\n';
                    });
                    errorMessage += 'Please Try Again';
                    alert(errorMessage);
                }
            }).fail(function() {
                alert('There Was an Error Connecting to the Server. Please Try Again.');
            }).always(function() {
                loadStub();
            });
        });
        if (typeof callback == 'function') {
            callback();
        }
    }).fail(function() {
        $(project).html('<div style="margin: 75px 0; text-align: center;">There Was an Error Connecting to the Server<br/>Please Try Again</div>');
    });
}

我能够确定通过使用 setContent 方法而不是分配 InfoWindow.content 谷歌地图会适当地调整 InfoWindow 大小,但它不会像使用 InfoWindow.open 时那样平移地图以适应 InfoWindow 的内容()。有谁知道一个体面的解决方法?

如果我需要澄清任何事情,请告诉我。

提前致谢。

4

2 回答 2

4

尝试调用 InfoWindow.open(gmap, newMarker); 在您将内容设置为ajax结果之后再次。当根据内容大小调用 open 方法时,地图将自动平移,这似乎是一个可用的解决方法。

于 2013-09-17T09:18:37.490 回答
3

即使您的信息窗口内容是动态的,您也可以选择接近加载内容时的值的宽度和高度值。

InfoWindow.content = $('<div class="projectStub" guid=""></div>').attr('guid', guid).get(0);

这里尝试给出projectSub一个宽度和高度值。

您可以做的另一件事是:将地图移动到带有活动信息窗口的标记位于中心的位置。这样,用户点击后标记会移动一点,但您的信息窗口肯定会完全可见。

loadStub($('div.projectStub[guid="' + guid + '"]').get(), guid, false, function() {
    InfoWindow.setContent(InfoWindow.content);
    gmap.panTo(newMarker.getPosition());
});
于 2013-06-20T20:09:25.397 回答