简体   繁体   English

单击Google Maps标记时添加infoWindow气泡

[英]Add infoWindow bubble on clicking Google Maps marker

I'd like to add an infoWindow so when I click a marker it displays the title & description in a nice little window bubble. 我想添加一个infoWindow,以便在单击标记时在一个漂亮的小窗口气泡中显示标题和说明。

Using my existing code, can someone give me a little help with how to achieve this? 使用我现有的代码,有人可以在实现该目标方面给我一些帮助吗?

'park_data' is an array I'm storing the title, longitude & latitude & description of each park in the city. “ park_data”是一个数组,用于存储城市中每个公园的标题,经度,纬度和描述。

Javascript 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 () {

    }
});

I'm using Google Maps 3.8.1 . 我正在使用Google Maps 3.8.1

My array 'park_data' is dynamically generated with PHP and looks a bit like this: 我的数组“ park_data”是用PHP动态生成的,看起来像这样:

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

Many thanks for any help. 非常感谢您的帮助。

You need to create an instance of the infowindow that you can use in the render function. 您需要创建可以在render函数中使用的信息窗口的实例。

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

You might need to create that in the initialize function the same way you create the park_data array. 您可能需要以与创建park_data数组相同的方式在initialize函数中创建该数组。

You need to set the content of the infowindow (we don't know what you want here but here is an example) 您需要设置信息窗口的内容(我们在这里不知道您想要什么,但这是一个示例)

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!');

You need an event listener on your marker to open the infowindow on click (put this code after the above code): 您需要在标记上使用事件侦听器,以在单击时打开信息窗口(将此代码放在上面的代码之后):

google.maps.event.addListener(marker, 'click', function () {

    this.model.infowindow.open(map, marker);
});

As I said, I don't know about backbone.js so I am not sure about how you are supposed to declare / retrieve your variables, but that should help you get started. 就像我说的,我不了解lobb.js,所以我不确定应该如何声明/检索变量,但这应该可以帮助您入门。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM