Guest User

czukowski

a guest
Nov 27th, 2009
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Circular Sector Overlay for Google Maps API v3 MooTools 1.2 Class, based on CircleOverlay
  3.  *
  4.  * Arguments:
  5.  *  map -- the google.maps.Map object instnace
  6.  *  point -- the center point of the circle, google.maps.LatLng object
  7.  *  radius -- radius of the circle, in meters, number
  8.  *  angle -- sector angle (theta as in fig. here: http://en.wikipedia.org/wiki/Circular_sector), in degrees
  9.  *  direction -- a point, which the sector faces, google.maps.LatLng object
  10.  *  options -- various options, mainly drawing
  11.  */
  12. var CircularSectorOverlay = new Class({
  13.     Implements: [Options],
  14.     options: {
  15.         numPoints: 100, // for 360 deg.
  16.         drawOptions: {
  17.             strokeColor: "#FF0000",
  18.             strokeOpacity: 0.8,
  19.             strokeWeight: 2,
  20.             fillColor: "#FF0000",
  21.             fillOpacity: 0.35
  22.         }
  23.     },
  24.  
  25.     initialize: function(map, center, radius, angle, direction, options) {
  26.         this.setOptions(options);
  27.  
  28.         google.maps.OverlayView.call(this);
  29.         var q = 180 / 63781370 / Math.sin(90 - center.lat()) / Math.PI;
  30.  
  31.         this._map = map;
  32.         this._point = center;
  33.         this._angle = angle;
  34.         this._direction = direction;
  35.         this._radius = radius * q * 10; // convert meters to latlang degrees
  36.  
  37.         // Fit bounds of a sector that is drawn into the map
  38.         this.latlngbounds = new google.maps.LatLngBounds();
  39.         this.sectorLatLngs = new Array();
  40.         this.sectorLatLngs.push(this._point);
  41.  
  42.         var numPointsSector = Math.ceil(this.options.numPoints * angle / 360);
  43.         var angleIncrement = this._angle / numPointsSector;
  44.         var startAngle = Math.asin(2 * (center.lat() - direction.lat()) / 2) * Math.PI - angle / 2;
  45.  
  46.         var d2r = Math.PI / 180;
  47.         var circleLat = this._radius;
  48.         var circleLng = circleLat / Math.cos(center.lat() * d2r);
  49.  
  50.         for (var i = 0; i < numPointsSector; i++) {
  51.             var theta = startAngle + i * angleIncrement;
  52.             var vertexLat = center.lat() + circleLat * Math.sin(theta * d2r);
  53.             var vertexLng = center.lng() + circleLng * Math.cos(theta * d2r);
  54.             var vertextLatLng = new google.maps.LatLng(vertexLat, vertexLng);
  55.             this.sectorLatLngs.push(vertextLatLng);
  56.             this.latlngbounds.extend(vertextLatLng);
  57.         }
  58.         map.fitBounds(this.latlngbounds);
  59.         this.set_map(map);
  60.     },
  61.    
  62.     onAdd: function() {
  63.         var polyOptions = this.options.drawOptions;
  64.         polyOptions.paths = this.sectorLatLngs;
  65.         this.polygon = new google.maps.Polygon(polyOptions);
  66.         this.polygon.setMap(this.map);
  67.     },
  68.  
  69.     onRemove: function() {
  70.         this.polygon.setMap(null);
  71.     },
  72.    
  73.     draw: function() {
  74.         this.onRemove();
  75.         this.onAdd();
  76.     }
  77. });
  78. CircularSectorOverlay.implement(new google.maps.OverlayView());
Advertisement
Add Comment
Please, Sign In to add comment