【问题标题】:How to make infowindows have tabs in Google Map?如何让信息窗口在谷歌地图中有标签?
【发布时间】:2012-06-11 19:40:38
【问题描述】:

如何使我的 infoWindows 具有标签内容?我试过这样的事情:

google.maps.event.addListener(this, "domready", function(){ $("#info").tabs() });

*还尝试使用infoWidnwowinfowindowiw 代替this 关键字

.ready(function(){ $("#info").tabs();});

.bind('domready', function(){ $("#info").tabs(); });

这些都不起作用。

创建标记和信息窗口的代码:

$('#map').gmap(mapOptions).bind('init', function(){
    $.post('getmarkers.php', function(json){
        var theMarkers = json;
        $.each(theMarkers, function(i, element) {
            $.each(element, function(object, attributes){
                $('#map').gmap('addMarker', { 
                    'position': new google.maps.LatLng(parseFloat(attributes.Lat), parseFloat(attributes.Lng)), 
                    'bounds':true } ).click(function(){                             
                        $('#map').gmap('openInfoWindow', { 'content':'<div id="info"><ul><li><a href="#tab1">Tab1</li><li><a href="#tab2">Tab2</li></ul><div id="tab1"><h1>'+attributes.productName+'</h1></div><div id="tab2"><h2 style="color: grey"><h1>'+attributes.productPrice+'</h1></div></div>' }, this);                        
                }); 
            });
        });
    }); 
});

不知何故我需要告诉这部分:

$('#map').gmap('openInfoWindow', { 'content':'<div id="info"><ul><li><a href="#tab1">Tab1</li><li><a href="#tab2">Tab2</li></ul><div id="tab1"><h1>'+attributes.productName+'</h1></div><div id="tab2"><h2 style="color: grey"><h1>'+attributes.productPrice+'</h1></div></div>' }, this);      

标签content 我传递给openInfoWindow

【问题讨论】:

  • 我之前实际上并没有尝试过这样做,但是您不会在创建信息窗口而不是在初始化时提供信息窗口选项卡吗?在初始化时,信息窗口不存在。另外,是否可以有多个信息窗口?如果是这样,那么您使用的 id 将不是唯一的。
  • 看起来你来自熟悉 v2 api,其中内置了 infowindow 选项卡。不幸的是,它从未进入 v3 api,所以你必须,正如 Kevin 所说,在打开时创建选项卡信息窗口。如果您不熟悉自己创建选项卡式界面,可以查看类似 jQuery UI 的工具来帮助您。
  • @Kevin B:嗯,不。我使用 opneInfoWindow 函数调用了所有这些函数。至于唯一性,你是说我应该尝试使用 CSS 类而不是 id?
  • @Adam:不是真的。从第 3 版开始,我一直在使用 Google 地图。我从未接触过 V2 或更低版本。

标签: javascript jquery google-maps google-maps-api-3 jquery-ui-map


【解决方案1】:

有一个免费的 jquery 插件可以处理多个选项卡、每个选项卡的定位和样式,我刚刚发现它有一个直观的自定义界面。这是一个演示

https://github.com/googlemaps/js-info-bubble/blob/gh-pages/examples/example.html

【讨论】:

    【解决方案2】:

    删除您发布的前三个 sn-ps 并使用它:

    $('#map').gmap(mapOptions).bind('init', function() {
        $.post('getmarkers.php', function(json) {
            var theMarkers = json;
            $.each(theMarkers, function(i, element) {
                $.each(element, function(object, attributes) {
                    $('#map').gmap('addMarker', {
                        'position': new google.maps.LatLng(parseFloat(attributes.Lat), parseFloat(attributes.Lng)),
                        'bounds': true
                    }).click(function() {
                        var ts = $.now();
                        $('#map').gmap('openInfoWindow', {
                            "<div id='" + ts + "info'>... your tab content ..."
                        }, this);
                        $('#' + ts + 'info').tabs();
                    });
                });
            });
        });
    });​
    

    我创建了一个唯一的字符串并用它给 div 一个唯一的 id,然后用它来制作标签。

    【讨论】:

    • 我可以用class代替id作为主div吗?
    • 你可以,但是你如何选择一个信息窗口而不选择它们呢?您没有可以使用的上下文。
    • 但是当我点击一个标记时,地图不知道应该调用哪个信息框吗?这就是我在 openInfoWindow 函数末尾指定的内容 - this 关键字,它告诉它将此内容附加到我们正在处理的当前标记。仅供参考,我正在使用 jquery ui map 插件来处理 Google 地图。
    • 啊,我没有注意到你使用的 jQuery 插件语法,我从来没有使用过那个插件。我会做一些研究,你能给我一个链接以节省时间吗?
    • "但是当我单击标记时,地图不知道应该调用哪个信息框吗?"不,因为在插件使用 google maps api 创建它之前,infowindow 不存在,但它不会返回对它的引用。
    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 2012-11-20
    • 2011-08-21
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多