2014-01-06 44 views
0

我把示例代碼從這裏--->https://google-developers.appspot.com/maps/articles/mvcfun/step6調整大小圓(MVC對象)

,我已經爲了有一個最大和最小尺寸調整範圍增加了一些代碼.. 我成功使這兩個範圍的圈子停止,但調整大小標記不會停止,如圓...

我已經改變了唯一的事情就是從這個

RadiusWidget.prototype.setDistance = function() { 
    // As the sizer is being dragged, its position changes. Because the 
    // RadiusWidget's sizer_position is bound to the sizer's position, it will 
    // change as well. 
    var pos = this.get('sizer_position'); 
    var center = this.get('center'); 
    var distance = this.distanceBetweenPoints_(center, pos); 

    // Set the distance property for any objects that are bound to it 
    this.set('distance', distance); 
}; 

這個

RadiusWidget.prototype.setDistance = function() { 
     // As the sizer is being dragged, its position changes. Because the 
     // RadiusWidget's sizer_position is bound to the sizer's position, it will 
     // change as well. 
     var min=0.5; 
     var max=15; 
     var pos = this.get('sizer_position'); 
     var center = this.get('center'); 
     var distance = this.distanceBetweenPoints_(center, pos); 
     if (distance < min){ distance = min;} 
     if (distance > max){distance = max;} 





     // Set the distance property for any objects that are bound to it 
     this.set('distance', distance); 
     }; 

回答

2

如果我把距離限制在「拖」事件監聽器爲我工作得更好:

google.maps.event.addListener(sizer, 'drag', function() { 
     // As the sizer is being dragged, its position changes. Because the 
     // RadiusWidget's sizer_position is bound to the sizer's position, it will 
     // change as well. 
     var min=0.5; 
     var max=15; 
     var pos = me.get('sizer_position'); 
     var center = me.get('center'); 
     var distance = google.maps.geometry.spherical.computeDistanceBetween(center, pos)/1000; 
     if (distance < min) { 
     me.set('sizer_position', google.maps.geometry.spherical.computeOffset(center,min*1000,google.maps.geometry.spherical.computeHeading(center,pos))); 
     } else if (distance > max){ 
     me.set('sizer_position', google.maps.geometry.spherical.computeOffset(center,max*1000,google.maps.geometry.spherical.computeHeading(center,pos))); 
     } 
     // Set the circle distance (radius) 
     me.setDistance(); 
    }); 
    }; 

注:我用spherical geometry computeDistanceBetween method計算的距離),需要包括庫。

working example

+0

ooh非常感謝你!這對我來說非常合適!保持良好的工作!再次感謝! –