信息窗口在同一标记处打开!
好吧,我有这个代码,它会为我从数据库获取的每个位置调用,问题是每当我单击一个标记时,信息窗口都会显示相应的标记信息,但信息窗口的位置总是避免最后一个添加了标记,简而言之,信息很好,但没有显示在应该在的位置。
这两个是在地图初始化时声明的,并且是全局的。
var infowindow = new google.maps.InfoWindow();
var geocoder=new google.maps.Geocoder
这是脚本,
function addRoleMarker(lat,lng,rumbo,codigo,velocidad,nE,referer,utc,fecha)
{
var myLatLng = new google.maps.LatLng(lat, lng);
var title='No.'+nE+' '+utc;
baseMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: title,
zIndex: 1
});
google.maps.event.addListener(baseMarker,'click',function(){
geocoder.geocode({'latLng': myLatLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
switch(rumbo)
{
case 0:
rumbo='N';
break;
case 1:
rumbo='NE';
break;
case 2:
rumbo='E';
break;
case 3:
rumbo='SE';
break;
case 4:
rumbo='S';
break;
case 5:
rumbo='SO';
break;
case 6:
rumbo='O';
break;
case 7:
rumbo='NO';
break;
}
var tablaR="<table><tr><td>Fecha:</td></tr><td>"+fecha;
tablaR+="</td><tr><td>Fecha UTC:</td></tr><td>"+utc;
tablaR+="</td><tr><td>Velocidad:</td></tr><td>"+velocidad;
tablaR+="</td><tr><td>Rumbo:</td></tr><td>"+rumbo;
tablaR+="</td><tr><td>Direccion:</td></tr><td>"+results[0].formatted_address;
infowindow.setContent(tablaR);
infowindow.open(map,baseMarker);
}else{
alert("No results found");
}
}else{
alert("Geocoder failed due to: " + status);
}});});
}
标记的数量取决于根据 sql 查询获取的数据,这是 php 脚本
<?
$script="<script type='text/javascript'>";
for($i=0;$i<count($losDatos);$i++)
{
$script.="addRoleMarker(".$losDatos[$i]['latitud'].",".$losDatos[$i]['longitud'].",".$losDatos[$i]['rumbo'].",".$losDatos[$i]['codigo'].",".$losDatos[$i]['velocidad'].",".$losDatos[$i]['numeroEconomico'].",1,'".$losDatos[$i]['utcDate']."','".$losDatos[$i]['localDate']."');";
}
$script.='</script>';
echo $script;
?>
ok well , i have this code which is called for every position i fetch from a DB, the thing is that whenever i click on a marker the infowindow is shown with the respective marker information , but the position of the infowindow is always avobe the last marker added , in a nut shell info is fine but its not showed where it should be.
these two are declared when the map its initialized and are global.
var infowindow = new google.maps.InfoWindow();
var geocoder=new google.maps.Geocoder
this is the script
function addRoleMarker(lat,lng,rumbo,codigo,velocidad,nE,referer,utc,fecha)
{
var myLatLng = new google.maps.LatLng(lat, lng);
var title='No.'+nE+' '+utc;
baseMarker = new google.maps.Marker({
position: myLatLng,
map: map,
title: title,
zIndex: 1
});
google.maps.event.addListener(baseMarker,'click',function(){
geocoder.geocode({'latLng': myLatLng}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
switch(rumbo)
{
case 0:
rumbo='N';
break;
case 1:
rumbo='NE';
break;
case 2:
rumbo='E';
break;
case 3:
rumbo='SE';
break;
case 4:
rumbo='S';
break;
case 5:
rumbo='SO';
break;
case 6:
rumbo='O';
break;
case 7:
rumbo='NO';
break;
}
var tablaR="<table><tr><td>Fecha:</td></tr><td>"+fecha;
tablaR+="</td><tr><td>Fecha UTC:</td></tr><td>"+utc;
tablaR+="</td><tr><td>Velocidad:</td></tr><td>"+velocidad;
tablaR+="</td><tr><td>Rumbo:</td></tr><td>"+rumbo;
tablaR+="</td><tr><td>Direccion:</td></tr><td>"+results[0].formatted_address;
infowindow.setContent(tablaR);
infowindow.open(map,baseMarker);
}else{
alert("No results found");
}
}else{
alert("Geocoder failed due to: " + status);
}});});
}
the number of markers depends on the data fetched acording to a sql query this is the php script
<?
$script="<script type='text/javascript'>";
for($i=0;$i<count($losDatos);$i++)
{
$script.="addRoleMarker(".$losDatos[$i]['latitud'].",".$losDatos[$i]['longitud'].",".$losDatos[$i]['rumbo'].",".$losDatos[$i]['codigo'].",".$losDatos[$i]['velocidad'].",".$losDatos[$i]['numeroEconomico'].",1,'".$losDatos[$i]['utcDate']."','".$losDatos[$i]['localDate']."');";
}
$script.='</script>';
echo $script;
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我需要查看整个代码才能提出建议,但似乎您只向一个“baseMarker”添加监听器。因此,您的 infoWindow.open(map, baseMarker) 在同一标记处打开。我认为您需要为您创建的每个标记添加侦听器。
I need to see whole code to make suggestions but it seems you only add listener to one "baseMarker". So your infoWindow.open(map, baseMarker) opens at the same marker. I think you need to add listener to every marker you create.