2017-08-16 38 views
1

我试图让一个算法,只显示在一个给定的半径这个标记是我的代码:仅显示在给定的半径标记

for(i = 0; i < markers.length; i++) { 
 
    d = google.maps.geometry.spherical.computeDistanceBetween(
 
     new google.maps.LatLng(user_logged_in[0][0],user_logged_in[0][1]), 
 
     new google.maps.LatLng(markers[i][0], markers[i][1])); 
 
    var position = new google.maps.LatLng(markers[i][0], markers[i][1]); 
 
    var icon = {url:"http://".concat(markers[i][3]), 
 
       scaledSize: new google.maps.Size(20, 20), // scaled size 
 
       origin: new google.maps.Point(0,0), // origin 
 
       anchor: new google.maps.Point(0, 0) }; 
 
    marker = new google.maps.Marker({ 
 
     position: position, 
 
     map: map, 
 
     title: markers[i][4].toString().concat(' ').concat(markers[i][5].toString()), 
 
     icon: icon, 
 
     url:'im:<sip:'+markers[i][6]+'>' 
 
    }); 
 
    if(<?=$this->translate($this->layout()->is_super_admin)?>==0){ 
 
     marker.setVisible(false); 
 
     $(document).ready(function() { 
 
      $('#select-distance').change(function() { 
 
       var e = document.getElementById("select-distance"); 
 
       var value = parseInt(e.options[e.selectedIndex].value); 
 
       if(d < value){      
 
        marker.setVisible(true); 
 
       }else{ 
 
        marker.setVisible(false); 
 
       } 
 
       circle.setRadius(value); 
 
       circle.bindTo('center', markerc, 'position'); 
 
       find_closest_marker(value); 
 
      }); 
 
     }); 
 
    } 
 
}

但是当我测试了一下,只能对于给定半径中的最后一个用户。例如,如果我有3个距离为2km,3km,1km的用户,并且我选择了5km的半径,那么我只能得到3公里的用户!感谢帮助。

回答

0

制作每个标记的新对象并使用该对象。

见下图:

//here use the var keyword and a new marker object 
var marker[i] = new google.maps.Marker({ 
     position: position, 
     map: map, 
     title: markers[i][4].toString().concat(' ').concat(markers[i][5].toString()), 
     icon: icon, 
     url:'im:<sip:'+markers[i][6]+'>' 
    }); 

    if(<?=$this->translate($this->layout()->is_super_admin)?>==0){ 
     marker[i].setVisible(false); 
     $(document).ready(function() { 
      $('#select-distance').change(function() { 
      var e = document.getElementById("select-distance"); 
      var value = parseInt(e.options[e.selectedIndex].value); 
      if(d < value){      
       marker[i].setVisible(true); 
      }else{ 
       marker[i].setVisible(false); 
      } 
      circle.setRadius(value); 
      circle.bindTo('center', markerc, 'position'); 
      find_closest_marker(value); 
      }); 
     }); 

您使用的是同一个对象每次所以它正在取代前一个。这就是为什么它只绘制最后一个标记。

+0

我得到这个错误未捕获TypeError:无法读取未定义的属性'addListener !!!!! –

+0

@samersboui和你得到这个错误的行。 –

+0

var infowindow = new google.maps.InfoWindow({ content:'' }); 标记[I] .addListener( '鼠标悬停',函数(){ infowindow.setContent( '

'+ this.title +'

'+ '
Call
'); infowindow.open(地图,this); }); bounds.extend(position); } map.fitBounds(bounds); –

相关问题