Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将侧边栏链接到标记_Javascript_Google Maps_Google Maps Api 3_Sidebar - Fatal编程技术网

Javascript 如何将侧边栏链接到标记

Javascript 如何将侧边栏链接到标记,javascript,google-maps,google-maps-api-3,sidebar,Javascript,Google Maps,Google Maps Api 3,Sidebar,我正在尝试添加侧栏。我使用name变量作为侧边栏上的链接,这些链接是从数据库中获取的,我可以将其作为数组使用。我无法将QUERY\u LIMIT循环两次到google数据库 有人能给我提供如何使用这个功能的逻辑吗 这是我的剧本: <script type="text/javascript"> var markers = []; var side_html = ""; var icon1 = "prop.png"; var icon2 = "office.png";

我正在尝试添加侧栏。我使用
name
变量作为侧边栏上的链接,这些链接是从数据库中获取的,我可以将其作为数组使用。我无法将
QUERY\u LIMIT
循环两次到google数据库

有人能给我提供如何使用这个功能的逻辑吗

这是我的剧本:

<script type="text/javascript">

var markers = [];
var side_html = "";
var icon1 = "prop.png";
    var icon2 = "office.png";
    var locations = <?php echo $add_js ?>;
    var name = <?php echo $name_js ?>

    //function that will be used to handle clicks on the links in the sidebar
      function myclick(num) {
         google.maps.event.trigger(locations[num], "click");
      }

    function geocodeAddress(i) 
    {
        geocoder.geocode({'address' : locations[i]},
        function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
              map.setCenter(results[0].geometry.location);
              createMarker(results[0].geometry.location, i);
             } else {
             alert('Geocode was not successful for the following reason: ' + status);
                    }
            });
     }    

           function createMarker(latlng,i) {
           var marker = new google.maps.Marker({
                                    map : map,
                        icon : icon1,
                                    position : latlng
                });
     var infowindow = new google.maps.InfoWindow({
         size: new google.maps.Size(150,50),
         disableAutoPan: true
         });

      google.maps.event.addListener(marker, 'mouseover', function() {
           marker.setIcon(icon2);
           infowindow.setContent(locations[i]);
           infowindow.open(map, marker);

       });

       google.maps.event.addListener(marker, 'mouseout', function() {
              marker.setIcon(icon1);
              infowindow.setContent(locations[i]); 
              infowindow.close(map, marker);

         });


      return marker;
    }




    var map = new google.maps.Map(document.getElementById('map'), {
        zoom : 10,
        center : new google.maps.LatLng(28.6139, 77.2089),
        mapTypeId : google.maps.MapTypeId.ROADMAP
    });

    //var infowindow = new google.maps.InfoWindow({size: new google.maps.Size(150,50)});

    var geocoder = new google.maps.Geocoder();

    for (var i = 0 ; i < locations.length; i++) {
         geocodeAddress(i);


    // link to the sidebar HTML that will open the info window for the record being processed    
    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />';

    document.getElementById("side_bar").innerHTML = side_html;

    }//end of for loop
</script>

var标记=[];
var side_html=“”;
var icon1=“prop.png”;
var icon2=“office.png”;
var位置=;
变量名称=
//用于处理侧边栏中链接的单击的函数
函数myclick(num){
google.maps.event.trigger(位置[num],“单击”);
}
功能地理编码地址(i)
{
geocoder.geocode({'address':位置[i]},
功能(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
map.setCenter(结果[0].geometry.location);
createMarker(结果[0]。geometry.location,i);
}否则{
警报('地理编码因以下原因未成功:'+状态);
}
});
}    
函数createMarker(latlng,i){
var marker=new google.maps.marker({
地图:地图,
图标:icon1,
职位:latlng
});
var infowindow=new google.maps.infowindow({
尺寸:新谷歌地图尺寸(150,50),
真的吗
});
google.maps.event.addListener(标记'mouseover',函数(){
标记设置图标(icon2);
infowindow.setContent(位置[i]);
信息窗口。打开(地图、标记);
});
google.maps.event.addListener(标记'mouseout',函数(){
标记设置图标(icon1);
infowindow.setContent(位置[i]);
信息窗口。关闭(地图、标记);
});
返回标记;
}
var map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:新google.maps.LatLng(28.613977.2089),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
//var infowindow=new google.maps.infowindow({size:new google.maps.size(150,50)});
var geocoder=new google.maps.geocoder();
对于(变量i=0;i
我在输出中得到了正确的标记,但作为侧边栏链接,我每次都得到整个名称数组,而该链接甚至对单击事件都没有响应。

如果“名称”是一个数组,这应该可以工作:

    for (var i = 0 ; i < locations.length; i++) {
      geocodeAddress(i);
      // link to the sidebar HTML that will open the info window for the record being processed    
      side_html += '<a href="javascript:myclick(' + i + ')">' + name[i] + '</a><br />';
    }//end of for loop
    document.getElementById("side_bar").innerHTML = side_html;
for(变量i=0;i
终于解决了我的问题

我需要先拆分字符串数组,然后再使用它们显示在侧边栏链接中

  var name = names[i].split(",");
最终解决方案代码:

    for (var i = 0 ; i < locations.length; i++) 
    {
    geocodeAddress(i);

    var name = names[i].split(",");

    side_html += '<a href="javascript:myclick(' + i + ')">' + name + '</a><br />';
    }

    document.getElementById("side_bar").innerHTML = side_html;
for(变量i=0;i
你能给我一个从DB获得的数据的例子吗?它给我侧边栏链接中所有的名称数组“var name”数组都有名称,例如[“Janakpuri地区”、“Noida地区”、“Dwarka地区”、“Gurgaon Subcity”]“var地区”有地址,例如[“Janak Puri,新德里,印度”,“Sector 63,Noida”,“Dwarka,新德里,印度”,“印度哈里亚纳古尔冈”]我想将侧边栏中的名称字段链接到标记的位置。我已经实现了这种方法,但它不起作用。它按字符而不是按字符串获取名称数组。因此,我将j a n a k作为链接,单击它们不会打开相应的信息窗口。当它按字符获取数组时,它可能是字符串而不是数组