谷歌地图圈不绑定到数组中的所有标记

时间:2015-05-05 20:23:38

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

圆圈仅设置为我的数组中的最后一个标记,我如何设置圆圈以绑定到我的所有制作者。

如果用户制造商通过getBounds()方法输入圆的半径,是否可以让圆圈显示警告?因为我已将地理位置添加到我的代码中,用于跟踪用户位置,一旦他们输入半径,就需要显示警报消息。

var marker_school, ss;

    for (ss = 0; ss < location_school2.length; ss++) {

    marker_school = new google.maps.Marker({ 

         position: new google.maps.LatLng(location_school2[ss][1], location_school2[ss][2]),
         map: map, icon: school, animation: google.maps.Animation.BOUNCE, visible: false,


    });


    google.maps.event.addListener(marker_school, 'click', (function(marker_school, ss) {
         return function() {
             $("#school_bg").show();
             $("#name_school").append(location_school2[ss][3]);

         }
    })(marker_school, ss));
    markers2.push(marker_school);

}

google.maps.event.addListener(map, 'zoom_changed', function() {
     var currentZoom = map.getZoom();
    // iterate over markers and call setVisible
    if (currentZoom >= 14 && currentZoom <= 17) {
       for (ss = 0; ss < location_school2.length; ss++) {
        markers2[ss].setVisible(true);

       }
   }else if (currentZoom >= 8 && currentZoom <= 14) 
     {for (ss = 0; ss < location_school2.length; ss++) {
        markers2[ss].setVisible(false);

       }}
}); 

var circle = new google.maps.Circle({
  map: map,
  radius: 500,    // 10 miles in metres
  fillColor: '#AA0000', 
  strokeColor: '#FFF', strokeWeight: 0,
});
circle.bindTo('center', marker_school, 'position'); 

1 个答案:

答案 0 :(得分:0)

如果要将圆圈绑定到所有标记,则必须为每个标记创建一个圆,并将每个圆绑定到其自己的标记(在循环内)。请注意,距离5英里不到500米。

for (ss = 0; ss < location_school2.length; ss++) {

  var marker_school = new google.maps.Marker({ 
     position: new google.maps.LatLng(location_school2[ss][1], location_school2[ss][2]),
     map: map, icon: school, animation: google.maps.Animation.BOUNCE, visible: false,
  });
  var circle = new google.maps.Circle({
     map: map,
     radius: 500,    // 10 miles in metres
     fillColor: '#AA0000', 
     strokeColor: '#FFF', strokeWeight: 0,
  });
  circle.bindTo('center', marker_school, 'position'); 

  google.maps.event.addListener(marker_school, 'click', (function(marker_school, ss) {
     return function() {
         $("#school_bg").show();
         $("#name_school").append(location_school2[ss][3]);

     }
  })(marker_school, ss));
  markers2.push(marker_school);
}

working fiddle