【问题标题】:Show only markers in given radius仅显示给定半径内的标记
【发布时间】:2017-08-16 13:23:33
【问题描述】:

我试图制作一个只显示给定半径内的标记的算法,这是我的代码:

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 个距离为 2 公里、3 公里、1 公里的用户,我选择半径为 5 公里,我只得到 3 公里的用户!!!感谢您的帮助。

【问题讨论】:

    标签: javascript google-maps google-maps-api-3 maps


    【解决方案1】:

    为每个标记创建新对象并使用该对象。

    见下文:

     //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);  
               });
            });
    

    您每次都使用相同的对象,因此它正在替换之前的对象。这就是为什么它只绘制最后一个标记。

    【讨论】:

    • 我收到了这个错误 Uncaught TypeError: Cannot read property 'addListener' of undefined !!!!
    • @samersboui 以及出现此错误的行。
    • var infowindow = new google.maps.InfoWindow({ content: '' }); marker[i].addListener('mouseover', function() { infowindow.setContent('

      '+this.title+'

      ' + '
      调用
      '); infowindow.open(map, this); }); bounds.extend(位置); } map.fitBounds(bounds);
    • 我将它从 marker.addListener 编辑到 marker[i].addListener 并且我得到一个新错误:Cannot read property '0' of undefined at the same ligne
    • @samersboui 尝试从标记中删除 [i] 并仅添加 var 关键字
    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 2014-01-16
    • 1970-01-01
    • 2012-05-04
    • 2018-09-25
    • 2018-11-26
    • 2017-01-26
    • 2015-11-17
    相关资源
    最近更新 更多