代码之家  ›  专栏  ›  技术社区  ›  michaelmcgurk

单击谷歌地图标记时添加infoWindow气泡

  •  0
  • michaelmcgurk  · 技术社区  · 12 年前

    我想添加一个 信息窗口 所以当我单击标记时,它会显示标题&在一个漂亮的小窗口泡泡中描述。

    使用我现有的代码,有人能给我一点帮助吗?

    “park_data”是我存储标题、经度和经度的数组;纬度&城市中每个公园的描述。

    Java脚本

        // controls the display of the park (the result)
        var ParkView = Backbone.View.extend({
        tagName: "article",
        className: "park-container",
        template: $("#parkTemplate").html(),
    
        render: function () {
    
            console.log(window.map)
            var marker = new google.maps.Marker({
              map: window.map,
              position: new google.maps.LatLng(this.model.get("lat"), this.model.get("lng")),
              title: this.model.get("title"),
              icon: window.park_icon
            });
    
            if(this.model.collection.length == 1){
              window.map.setCenter(new google.maps.LatLng(this.model.get("lat"), this.model.get("lng")))
            } else {
    
                var latlng = new google.maps.LatLng(marker.position.lat(), marker.position.lng() );
                bounds.extend(latlng);
    
            }
    
            console.log("map - here!")
    
            var tmpl = _.template(this.template);
            //console.log(this.model)
            this.$el.html(tmpl(this.model.toJSON()));
            return this;
        }
    });
    
    
    var MapView = Backbone.View.extend({
    
        initialize: function () {
            this.collection = new ParkCollection(park_data); //// array is generated in a separate file
            this.render();
    
        },
    
        render: function () {
    
        }
    });
    

    我正在使用 谷歌地图3.8.1 .

    我的数组“park_data”是用PHP动态生成的,看起来有点像这样:

    var park_data = [{"title":"Central Park", "lat":"55.8546658", "lng":"-4.241034300000024", "desc":"Description goes here"}]
    

    非常感谢您的帮助。

    1 回复  |  直到 12 年前
        1
  •  1
  •   MrUpsidown    12 年前

    您需要创建一个可以在渲染函数中使用的infowindow实例。

    var infowindow = new google.maps.InfoWindow();
    

    您可能需要以创建 park_data 大堆

    您需要设置信息窗口的内容(我们不知道您想要什么,但这里有一个示例)

    var marker = new google.maps.Marker({
        map: window.map,
        position: new google.maps.LatLng(this.model.get("lat"), this.model.get("lng")),
        title: this.model.get("title"),
        icon: window.park_icon
    });
    
    // Access the infowindow that you created in step 1 and set its content
    // That might be something like that:
    
    this.model.infowindow.setContent('Hello World!');
    

    您需要在标记上设置一个事件侦听器,以便在单击时打开信息窗口(将此代码放在上述代码之后):

    google.maps.event.addListener(marker, 'click', function () {
    
        this.model.infowindow.open(map, marker);
    });
    

    正如我所说,我不知道backline.js,所以我不确定应该如何声明/检索变量,但这应该有助于您开始。

    希望这有帮助!