如何使用Array中的值标记管理器

时间:2015-10-19 12:30:35

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

我在谷歌搜索过如何在数组中使用带有值的Marker Manager我在数组中有很多带有经度和纬度信息的Marker看到示例代码如下所示

                var mgr;
                var map;
            function initialize(){

                  var mapOptions = {
                                   zoom: 4,
                                   center: new google.maps.LatLng(28.975750, 10.669184),
                                   mapTypeId: google.maps.MapTypeId.SATELLITE
                               };
                        map = new google.maps.Map(document.getElementById('resultcnn'), mapOptions);
                        mgr = new MarkerManager(map);

                   var locations = [  
                            ['rabat', 33.906896,-6.263123],
                            ['rabat2', 34.053993,-6.792237],
                            ['agdal', 33.994469,-6.848702],
                            ['casa', 33.587596,-7.657156],  
                            ['casa2', 33.531808,-7.674601],  
                            ['casa3', 33.58824,-7.673278],  
                            ['casa4', 33.542325,-7.578557],  
                            ['agadir', 30.433948,-9.600005],  
                            ['kech', 31.634676,-8.000164],  
                            ['oujda,', 34.689969,-1.912365],
                            ['tanger,', 35.771586,-5.801868],
                            ['JIJEL-ACHOUAT',36.8,5.883333333,1],
                            ['JIJEL-PORT',36.81666667,5.883333333,2],
                            ['SKIKDA',36.88333333,6.9,3],
                            ['ANNABA',36.83333333,7.816666667,4],
                            ['EL-KALA',36.9,8.45,8],
                            ['ALGER-PORT',36.76666667,3.1,9],
                            ['DELLYS',36.91666667,3.95,10.4],
                            ['DAR-EL-BEIDA',36.68333333,3.216666667,12.08571429],
                            ['TIZI-OUZOU',36.7,4.05,13.77142857]
                        ];

                                   google.maps.event.addListener(mgr, 'loaded', function() {
                                   for (var i = 0; i < locations.length; i++) {
                                       var marker = new google.maps.Marker({
                                           position: new google.maps.LatLng(locations[i][1], locations[i][2]),

                                           map: map
                                       });
                                       mgr.addMarkers(marker,5);
                                   }
                                   mgr.refresh();
                                  }); 


            }

我需要的是当我有页面时我只需要显示一些标记,当我缩放我的地图时,我想得到另一个标记我尝试了一些但是它没有用,请看下面的代码:

$(document).ready(function() {
   var now = new Date();
       $('#send-time').datetimepicker({
           format: 'yyyy/mm/dd hh:ii',
           startDate: now
       })
       .on('changeDay', function(ev){
           $('#send-time').datetimepicker('update',ev.date);
       });
    console.log('test'); 
});

谢谢

1 个答案:

答案 0 :(得分:1)

  1. 不要将标记添加到地图中,让MarkerManager执行该操作
  2. 如果您只添加一个标记,请使用.addMarker,而不是.addMarkers
  3. proof of concept fiddle

    代码段

    var mgr;
    var map;
    
    function initialize() {
    
      var mapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(34.6479355684, -0.2292535),
        mapTypeId: google.maps.MapTypeId.SATELLITE
      };
      map = new google.maps.Map(document.getElementById('resultcnn'), mapOptions);
    
      mgr = new MarkerManager(map);
    
      var locations = [
        ['rabat', 33.906896, -6.263123],
        ['rabat2', 34.053993, -6.792237],
        ['agdal', 33.994469, -6.848702],
        ['casa', 33.587596, -7.657156],
        ['casa2', 33.531808, -7.674601],
        ['casa3', 33.58824, -7.673278],
        ['casa4', 33.542325, -7.578557],
        ['agadir', 30.433948, -9.600005],
        ['kech', 31.634676, -8.000164],
        ['oujda,', 34.689969, -1.912365],
        ['tanger,', 35.771586, -5.801868],
        ['JIJEL-ACHOUAT', 36.8, 5.883333333, 1],
        ['JIJEL-PORT', 36.81666667, 5.883333333, 2],
        ['SKIKDA', 36.88333333, 6.9, 3],
        ['ANNABA', 36.83333333, 7.816666667, 4],
        ['EL-KALA', 36.9, 8.45, 8],
        ['ALGER-PORT', 36.76666667, 3.1, 9],
        ['DELLYS', 36.91666667, 3.95, 10.4],
        ['DAR-EL-BEIDA', 36.68333333, 3.216666667, 12.08571429],
        ['TIZI-OUZOU', 36.7, 4.05, 13.77142857]
      ];
    
      google.maps.event.addListener(mgr, 'loaded', function() {
        for (var i = 0; i < locations.length; i++) {
          var marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][1], locations[i][2]),
          });
          mgr.addMarker(marker, 5);
        }
        mgr.refresh();
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    html,
    body,
    #resultcnn {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js"></script>
    <div id="resultcnn"></div>