Javascript 如何在谷歌地图中自动打开特定标记的信息框?
我想根据给定的标记id自动打开标记的信息框Javascript 如何在谷歌地图中自动打开特定标记的信息框?,javascript,google-maps,google-maps-markers,Javascript,Google Maps,Google Maps Markers,我想根据给定的标记id自动打开标记的信息框 //Data of markers are from JSON var markers = new google.maps.Marker({ id: place.id, map: map, title:place.title, icon : 'assets/frontend/images/marker.png' }); infoWindow = new google.maps.InfoWindow
//Data of markers are from JSON
var markers = new google.maps.Marker({
id: place.id,
map: map,
title:place.title,
icon : 'assets/frontend/images/marker.png'
});
infoWindow = new google.maps.InfoWindow();
infoWindow.open(map, markers);
例如,假设我想打开id=243的标记的信息框
var marker_id = 243;
您可以对infobox使用
google-maps-utility-library-v3
,按如下方式修改代码,如果您有多个标记并且根据标记需要多个信息框,您可以重复infobox
的代码,首先尝试一个标记,您就有了想法
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox.js"></script>
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>
<script language="javascript">
//Data of markers are from JSON
$(document).ready(function(){
/*Your map code needs to be here*/
/*Marker being created below*/
var markers = new google.maps.Marker({
id: place.id,
map: map,
title:place.title,
icon : 'assets/frontend/images/marker.png'
});
var boxText = document.createElement("div");
boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
boxText.innerHTML = "Your map info here";
var myOptions_txtbox = {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: null
,boxStyle: {
background: "url('tipbox.gif') no-repeat"
,opacity: 0.75
,width: "280px"
}
,closeBoxMargin: "10px 2px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
/*Text box being created for marker `markers` */
var infobox_new = new InfoBox(myOptions_txtbox);
/*Created text box is assigned for marker `markers`. You can assign any event. For now, I have selected on `click` */
google.maps.event.addListener(markers, "click", function (e) {
infobox_new.open(map, this);
});
});
</script>
//标记的数据来自JSON
$(文档).ready(函数(){
/*你的地图代码必须在这里*/
/*正在下面创建的标记*/
var markers=new google.maps.Marker({
id:place.id,
地图:地图,
标题:place.title,
图标:“assets/frontend/images/marker.png”
});
var-boxText=document.createElement(“div”);
boxText.style.cssText=“边框:1px纯黑色;页边距顶部:8px;背景:黄色;填充:5px;”;
boxText.innerHTML=“此处的地图信息”;
变量myOptions\u txtbox={
内容:boxText
,disableAutoPan:false
,最大宽度:0
,pixelOffset:new google.maps.Size(-140,0)
,zIndex:null
,boxStyle:{
背景:“url('tipbox.gif')不重复”
,不透明度:0.75
,宽度:“280px”
}
,closeBoxMargin:“10px 2px 2px 2px”
,closeBoxURL:“http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance:newgoogle.maps.Size(1,1)
,isHidden:错
,窗格:“浮动窗格”
,enableEventPropagation:false
};
/*正在为标记“markers”创建文本框*/
var infobox_new=新infobox(myOptions_txtbox);
/*已为标记“markers”指定了已创建的文本框。您可以指定任何事件。目前,我已在“click”上进行了选择*/
google.maps.event.addListener(标记,“单击”,函数(e){
infobox_new.open(地图,这个);
});
});
您可以使用google-maps-utility-library-v3
创建infobox,按如下方式修改代码,如果您有多个标记,并且需要根据标记创建多个信息框,您可以重复创建infobox的代码,首先尝试使用单个标记,这样您就有了想法
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/src/infobox.js"></script>
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=true&libraries=places"></script>
<script language="javascript">
//Data of markers are from JSON
$(document).ready(function(){
/*Your map code needs to be here*/
/*Marker being created below*/
var markers = new google.maps.Marker({
id: place.id,
map: map,
title:place.title,
icon : 'assets/frontend/images/marker.png'
});
var boxText = document.createElement("div");
boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
boxText.innerHTML = "Your map info here";
var myOptions_txtbox = {
content: boxText
,disableAutoPan: false
,maxWidth: 0
,pixelOffset: new google.maps.Size(-140, 0)
,zIndex: null
,boxStyle: {
background: "url('tipbox.gif') no-repeat"
,opacity: 0.75
,width: "280px"
}
,closeBoxMargin: "10px 2px 2px 2px"
,closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance: new google.maps.Size(1, 1)
,isHidden: false
,pane: "floatPane"
,enableEventPropagation: false
};
/*Text box being created for marker `markers` */
var infobox_new = new InfoBox(myOptions_txtbox);
/*Created text box is assigned for marker `markers`. You can assign any event. For now, I have selected on `click` */
google.maps.event.addListener(markers, "click", function (e) {
infobox_new.open(map, this);
});
});
</script>
//标记的数据来自JSON
$(文档).ready(函数(){
/*你的地图代码必须在这里*/
/*正在下面创建的标记*/
var markers=new google.maps.Marker({
id:place.id,
地图:地图,
标题:place.title,
图标:“assets/frontend/images/marker.png”
});
var-boxText=document.createElement(“div”);
boxText.style.cssText=“边框:1px纯黑色;页边距顶部:8px;背景:黄色;填充:5px;”;
boxText.innerHTML=“此处的地图信息”;
变量myOptions\u txtbox={
内容:boxText
,disableAutoPan:false
,最大宽度:0
,pixelOffset:new google.maps.Size(-140,0)
,zIndex:null
,boxStyle:{
背景:“url('tipbox.gif')不重复”
,不透明度:0.75
,宽度:“280px”
}
,closeBoxMargin:“10px 2px 2px 2px”
,closeBoxURL:“http://www.google.com/intl/en_us/mapfiles/close.gif"
,infoBoxClearance:newgoogle.maps.Size(1,1)
,isHidden:错
,窗格:“浮动窗格”
,enableEventPropagation:false
};
/*正在为标记“markers”创建文本框*/
var infobox_new=新infobox(myOptions_txtbox);
/*已为标记“markers”指定了已创建的文本框。您可以指定任何事件。目前,我已在“click”上进行了选择*/
google.maps.event.addListener(标记,“单击”,函数(e){
infobox_new.open(地图,这个);
});
});
感谢您的回复,但我希望它是自动打开的,而不是单击事件。请给出建议。我认为您不会一次打开所有标记的所有信息框,所以我添加了click事件,如果您想打开所有这些,那么只需调用函数,删除addListener块,并用infobox_new.open(映射,标记)替换它;我只想打开一个有给定id的。没有更少,没有更多。但是它怎么知道将打开marker的哪个信息窗口呢?例如,我想打开标记id 243的信息窗口。感谢您的回复,但我希望它自动打开,而不是单击事件。请给出建议。我认为您不会一次打开所有标记的所有信息框,所以我添加了click事件,如果您想打开所有这些,那么只需调用函数,删除addListener块,并用infobox_new.open(映射,标记)替换它;我只想打开一个有给定id的。没有更少,没有更多。但是它怎么知道将打开marker的哪个信息窗口呢?例如,我想打开标记ID243的信息窗口。