Skip to content

Instantly share code, notes, and snippets.

@timcharper
Created May 4, 2009 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timcharper/106313 to your computer and use it in GitHub Desktop.
Save timcharper/106313 to your computer and use it in GitHub Desktop.
top_left = new GLatLng(point.lat() - radius, point.lng() - radius),
bottom_right = new GLatLng(point.lat() + radius, point.lng() + radius)
this.rectangle = new GRectangle(
this.map,
top_left,
bottom_right,
{
line_color: (this.kind == "home") ? "#3355ff" : "#ff5533",
fill_color: (this.kind == "home") ? "#335599" : "#995533",
line_opacity: 0.5,
line_weight: 2,
fill_opacity: 0.1,
dragend: function() {
$('commute_'+this.kind+'_lat_min').value = this.rectangle.lat1();
$('commute_'+this.kind+'_lat_max').value = this.rectangle.lat2();
$('commute_'+this.kind+'_long_min').value = this.rectangle.lng1();
$('commute_'+this.kind+'_long_max').value = this.rectangle.lng2();
}.bind(this)
}
);
GRectangle = Class.create();
GRectangle.prototype = {
initialize: function(map, top_left, bottom_right, options) {
this.options = $H({
line_color : "#0000ff",
line_opacity : 0.8,
line_weight : 3,
fill_color : "#7777ff",
fill_opacity : 0.3
}).merge(options);
this.map = map;
// this.markers = []
this.top_left = this.addHandle(top_left);
this.top_right = this.addHandle(new GLatLng(bottom_right.y, top_left.x));
this.bottom_left = this.addHandle(new GLatLng(top_left.y, bottom_right.x));
this.bottom_right = this.addHandle(bottom_right);
this.top_left.constraints = [this.top_right, this.bottom_left]
this.top_right.constraints = [this.top_left, this.bottom_right]
this.bottom_left.constraints = [this.bottom_right, this.top_left]
this.bottom_right.constraints = [this.bottom_left, this.top_right]
this.markers = [this.top_left, this.top_right, this.bottom_right, this.bottom_left]
this.drawPoly();
},
addHandle: function(point) {
// Square marker icons
var square = new GIcon();
square.image = "/images/square.png";
square.iconSize = new GSize(11, 11);
square.dragCrossSize = new GSize(0, 0);
square.shadowSize = new GSize(11, 11);
square.iconAnchor = new GPoint(5, 5);
// Make markers draggable
var marker = new GMarker(point, {icon:square, draggable:true, bouncy:false, dragCrossMove:true, zIndexProcess:importanceOrder});
marker.importance = 15;
this.map.addOverlay(marker);
// markers.push(marker);
GEvent.addListener(marker, "drag", function() {
marker.constraints[0].setLatLng(new GLatLng(marker.constraints[0].getLatLng().lat(), marker.getLatLng().lng()));
marker.constraints[1].setLatLng(new GLatLng(marker.getLatLng().lat(), marker.constraints[1].getLatLng().lng()));
this.drawPoly();
}.bind(this));
GEvent.addListener(marker, "dragend", function() {
if (this.options.get("dragend")) this.options.get("dragend")();
}.bind(this));
GEvent.addListener(marker, "mouseover", function() {
marker.setImage("/images/m-over-square.png");
});
GEvent.addListener(marker, "mouseout", function() {
marker.setImage("/images/square.png");
});
return marker;
},
drawPoly: function() {
if (this.polyShape) this.map.removeOverlay(this.polyShape);
polyPoints = this.markers.map(function(m) { return(m.getLatLng()) });
polyPoints.push(polyPoints.first());
this.polyShape = new GPolygon(polyPoints, this.options.get("line_color"), this.options.get("line_weight"), this.options.get("line_opacity"), this.options.get("fill_color"), this.options.get("fill_opacity"));
this.map.addOverlay(this.polyShape);
},
getBounds: function() {
p1 = this.top_right.getLatLng()
p2 = this.bottom_right.getLatLng()
return(new GLatLngBounds(
new GLatLng((p1.y < p2.y) ? p1.y : p2.y, (p1.x < p2.x) ? p1.x : p2.x),
new GLatLng((p1.y >= p2.y) ? p1.y : p2.y, (p1.x >= p2.x) ? p1.x : p2.x)
))
},
remove: function() {
this.markers.each(function(m) { this.map.removeOverlay(m) }.bind(this))
this.map.removeOverlay(this.polyShape);
this.markers.clear();
this.polyShape = null;
},
lat1: function() { return(this.top_left.getLatLng().lat()) },
lat2: function() { return(this.bottom_right.getLatLng().lat()) },
lng1: function() { return(this.top_left.getLatLng().lng()) },
lng2: function() { return(this.bottom_right.getLatLng().lng()) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment