Google地图中的变量infoWindow内容失败

时间:2016-10-13 19:38:33

标签: javascript google-maps

我有一个infoWindow,我想从变量中获取数据,但是每当我尝试运行它时,这段代码都会失败。

没有变量,此代码可以正常工作:

content: "<button onClick="MyObject.joinevent(this.id);" id='"+mid+"' class="join btn btn-primary pull-right">Join Event</button><br/><hr/>"+"<b>Event Title</b> "+eventtitle+"<br/>"+"<b>Event Time</b> "+eventtime+"<br/>"+"<b>Event Duration</b> "+eventduration+"<br/>"+"<b>Event Category</b> "+eventcategory

当我将内容分配给变量时:

  var infoWindow = new google.maps.InfoWindow({
var kontent = '"<button onClick="MyObject.joinevent(this.id);" id='"+mid+"' class="join btn btn-primary pull-right">Join Event</button><br/><hr/>"+"<b>Event Title</b> "+eventtitle+"<br/>"+"<b>Event Time</b> "+eventtime+"<br/>"+"<b>Event Duration</b> "+eventduration+"<br/>"+"<b>Event Category</b> "+eventcategory';

content: kontent
            });

我在行Unexpected token

中收到var kontent = ...错误

如何纠正我的问题?

2 个答案:

答案 0 :(得分:1)

InfoWindow收到一个哈希值。在外面定义您的变量,然后根据需要使用它:

var kontent = "<button onClick="MyObject.joinevent(this.id);" id='"+mid+"' class="join btn btn-primary pull-right">Join Event</button><br/><hr/>"+"<b>Event Title</b> "+eventtitle+"<br/>"+"<b>Event Time</b> "+eventtime+"<br/>"+"<b>Event Duration</b> "+eventduration+"<br/>"+"<b>Event Category</b> "+eventcategory;
var infoWindow = new google.maps.InfoWindow({
content: kontent
            });

答案 1 :(得分:1)

  

infowindows:InfoWindow构造函数接受InfoWindowOptions对象文字,它指定显示信息窗口的初始参数。

这意味着,您需要更改此内容:

 var infoWindow = new google.maps.InfoWindow({
        var kontent = '"<button onClick="MyObject.joinevent(this.id);" id='"+mid+"' class="join btn btn-primary pull-right">Join Event</button><br/><hr/>"+"<b>Event Title</b> "+eventtitle+"<br/>"+"<b>Event Time</b> "+eventtime+"<br/>"+"<b>Event Duration</b> "+eventduration+"<br/>"+"<b>Event Category</b> "+eventcategory';

        content: kontent
 });

要:

var kontent = '"<button onClick="MyObject.joinevent(this.id);" id='
        "+mid+"
        ' class="join btn btn-primary pull-right">Join Event</button><br/><hr/>"+"<b>Event Title</b> "+eventtitle+"<br/>"+"<b>Event Time</b> "+eventtime+"<br/>"+"<b>Event Duration</b> "+eventduration+"<br/>"+"<b>Event Category</b> "+eventcategory';

var infoWindow = new google.maps.InfoWindow({
      content: kontent
});