Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Combine all feature polygon drawing into one function

  • Loading branch information...
commit ea66eded83e067deea9b6424d5994844c0ace69c 1 parent 4367c11
@jsoma jsoma authored
Showing with 63 additions and 51 deletions.
  1. +28 −22 gmap-features.js
  2. +7 −7 gmap-features.min.js
  3. +28 −22 src/feature.js
View
50 gmap-features.js
@@ -171,24 +171,38 @@ var gmap = gmap || {};
}
return multipoly;
},
+ // Redraw the polygons associated with the feature
+ redraw: function() {
+ var opts = gmap._.extend({}, this.unselected_poly_options);
+
+ if(this._highlighted) {
+ opts = gmap._.extend(opts, this._highlighted_poly_options);
+ } else if(this._selected) {
+ opts = gmap._.extend(opts, this._selected_poly_options);
+ }
+
+ for (i=0,len=this.polygons.length; i<len; i++) {
+ this.polygons[i].setOptions(opts);
+ }
+ },
getSelected: function() {
return this._selected;
},
setSelected: function(value) {
var i, len;
if (value === true) {
- if (this.controller.selected !== null) { this.controller.selected.setSelected(false); }
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this._selected_poly_options);
- }
+ if (this.controller.selected !== null) {
+ this.controller.selected.setSelected(false);
+ }
this._selected = true;
- this.controller.selected = this;
- if (this.selectCallback) { this.selectCallback(); }
+ this.redraw();
+ this.controller.selected = this;
+ if (this.selectCallback) {
+ this.selectCallback();
+ }
} else if (value === false) {
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this.unselected_poly_options);
- }
this._selected = false;
+ this.redraw();
}
},
getHighlighted: function() {
@@ -198,21 +212,13 @@ var gmap = gmap || {};
var i,len;
if ((value === true) && (this._highlighted === false)) {
this._highlighted = true;
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this._highlighted_poly_options);
- }
- if (this.highlightCallback) { this.highlightCallback(); }
+ this.redraw();
+ if (this.highlightCallback) {
+ this.highlightCallback();
+ }
} else if ((value === false) && (this._highlighted === true)) {
this._highlighted = false;
- var opts;
- if (this.getSelected()) {
- opts = gmap._.extend({}, this.unselected_poly_options, this._selected_poly_options);
- } else {
- opts = gmap._.extend({}, opts, this.unselected_poly_options);
- }
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(opts);
- }
+ this.redraw();
}
}
};
View
14 gmap-features.min.js
@@ -1,12 +1,12 @@
if(!window.typeOf)window.typeOf=function(a){var b=typeof a;b==="object"&&(a?a instanceof Array&&(b="array"):b="null");return b};if(!window.console)window.console={log:function(){}};var gmap=gmap||{};
-(function(){gmap._=window._||function(){var a={},b={},g=Array.prototype.slice,c=Array.prototype.forEach,e=a.each=a.forEach=function(a,f,g){if(a!=null)if(c&&a.forEach===c)a.forEach(f,g);else if(a.length===+a.length)for(var e=0,i=a.length;e<i;e++){if(e in a&&f.call(g,a[e],e,a)===b)break}else for(e in a)if(hasOwnProperty.call(a,e)&&f.call(g,a[e],e,a)===b)break};a.extend=function(a){e(g.call(arguments,1),function(b){for(var c in b)b[c]!==void 0&&(a[c]=b[c])});return a};return a}();google.maps.LatLng.prototype.toGeoJSON=
-function(){return[this.lng(),this.lat()]};google.maps.MVCArray.prototype.toGeoJSON=function(){for(var a=this.getArray(),b=[],g=0,c=a.length;g<c;g++)b.push(a[g].toGeoJSON());return b};gmap.Feature=function(a){function b(){e.setHighlighted(!0)}function g(){e.setHighlighted(!1)}function c(){e.getSelected()?e.setSelected(!1):e.setSelected(!0)}var e=this;this.id=a.id;this.polygons=[];if(a.fields)this.fields=a.fields;this.controller=a.controller;this._highlighted=this._selected=!1;if(a.highlightCallback)this.highlightCallback=
+(function(){gmap._=window._||function(){var a={},b={},g=Array.prototype.slice,c=Array.prototype.forEach,e=a.each=a.forEach=function(a,f,g){if(a!=null)if(c&&a.forEach===c)a.forEach(f,g);else if(a.length===+a.length)for(var e=0,j=a.length;e<j;e++){if(e in a&&f.call(g,a[e],e,a)===b)break}else for(e in a)if(hasOwnProperty.call(a,e)&&f.call(g,a[e],e,a)===b)break};a.extend=function(a){e(g.call(arguments,1),function(b){for(var c in b)b[c]!==void 0&&(a[c]=b[c])});return a};return a}();google.maps.LatLng.prototype.toGeoJSON=
+function(){return[this.lng(),this.lat()]};google.maps.MVCArray.prototype.toGeoJSON=function(){for(var a=this.getArray(),b=[],g=0,c=a.length;g<c;g++)b.push(a[g].toGeoJSON());return b};gmap.Feature=function(a){function b(){e.setHighlighted(true)}function g(){e.setHighlighted(false)}function c(){e.getSelected()?e.setSelected(false):e.setSelected(true)}var e=this;this.id=a.id;this.polygons=[];if(a.fields)this.fields=a.fields;this.controller=a.controller;this._highlighted=this._selected=false;if(a.highlightCallback)this.highlightCallback=
a.highlightCallback;if(a.selectCallback)this.selectCallback=a.selectCallback;this.unselected_poly_options=a.color?gmap._.extend({},this._unselected_poly_options,{fillColor:a.color}):gmap._.extend({},this._unselected_poly_options);for(var d=0,f=a.multipolygon.length;d<f;d++)this.polygons.push(new google.maps.Polygon(gmap._.extend({},this.unselected_poly_options,{paths:a.multipolygon[d],map:a.map}))),google.maps.event.addListener(this.polygons[d],"mousemove",b),google.maps.event.addListener(this.polygons[d],
-"mouseout",g),google.maps.event.addListener(this.polygons[d],"click",c)};gmap.Feature.prototype={_unselected_poly_options:{clickable:!0,fillColor:"#AAAAAA",strokeColor:"#000000",strokeWeight:1,strokeOpacity:0.25},_selected_poly_options:{strokeOpacity:1,strokeWeight:1,strokeColor:"#0000FF"},_highlighted_poly_options:{strokeOpacity:1,strokeWeight:1,strokeColor:"#00FF00"},remove:function(){for(var a=0,b=this.polygons.length;a<b;a++)google.maps.event.clearListeners(this.polygons[a],"mousemove"),google.maps.event.clearListeners(this.polygons[a],
-"mouseout"),this.polygons[a].setMap(null);this.polygons=this.controller=null},toGeoJSON:function(){for(var a=[],b=0,g=this.polygons.length;b<g;b++)a.push(this.polygons[b].getPaths().toGeoJSON());return a},getSelected:function(){return this._selected},setSelected:function(a){var b;if(a===!0){this.controller.selected!==null&&this.controller.selected.setSelected(!1);for(a=0,b=this.polygons.length;a<b;a++)this.polygons[a].setOptions(this._selected_poly_options);this._selected=!0;this.controller.selected=
-this;this.selectCallback&&this.selectCallback()}else if(a===!1){for(a=0,b=this.polygons.length;a<b;a++)this.polygons[a].setOptions(this.unselected_poly_options);this._selected=!1}},getHighlighted:function(){return this._highlighted},setHighlighted:function(a){var b;if(a===!0&&this._highlighted===!1){this._highlighted=!0;for(a=0,b=this.polygons.length;a<b;a++)this.polygons[a].setOptions(this._highlighted_poly_options);this.highlightCallback&&this.highlightCallback()}else if(a===!1&&this._highlighted===
-!0){this._highlighted=!1;var g;g=this.getSelected()?gmap._.extend({},this.unselected_poly_options,this._selected_poly_options):gmap._.extend({},g,this.unselected_poly_options);for(a=0,b=this.polygons.length;a<b;a++)this.polygons[a].setOptions(g)}}}})();if(!window.typeOf)window.typeOf=function(a){var b=typeof a;b==="object"&&(a?a instanceof Array&&(b="array"):b="null");return b};if(!window.console)window.console={log:function(){}};gmap=gmap||{};gmap.geom=gmap.geom||{};
-gmap.geom.ParseGeoJSONMultiPolygon=function(a){var b,g,c,e,d,f,h=[],j,i;for(b=0,g=a.length;b<g;b++){j=[];for(c=0,e=a[b].length;c<e;c++){i=[];for(d=0,f=a[b][c].length;d<f;d++)i.push(new google.maps.LatLng(a[b][c][d][1],a[b][c][d][0]));j.push(i)}h.push(j)}return h};gmap.geom.ParseGeoJSONPolygon=function(a){var b,g,c,e,d=[],f;for(b=0,g=a.length;b<g;b++){f=[];for(c=0,e=a[b].length;c<e;c++)f.push(new google.maps.LatLng(a[b][c][1],a[b][c][0]));d.push(f)}return d};
+"mouseout",g),google.maps.event.addListener(this.polygons[d],"click",c)};gmap.Feature.prototype={_unselected_poly_options:{clickable:true,fillColor:"#AAAAAA",strokeColor:"#000000",strokeWeight:1,strokeOpacity:0.25},_selected_poly_options:{strokeOpacity:1,strokeWeight:1,strokeColor:"#0000FF"},_highlighted_poly_options:{strokeOpacity:1,strokeWeight:1,strokeColor:"#00FF00"},remove:function(){for(var a=0,b=this.polygons.length;a<b;a++)google.maps.event.clearListeners(this.polygons[a],"mousemove"),google.maps.event.clearListeners(this.polygons[a],
+"mouseout"),this.polygons[a].setMap(null);this.polygons=this.controller=null},toGeoJSON:function(){for(var a=[],b=0,g=this.polygons.length;b<g;b++)a.push(this.polygons[b].getPaths().toGeoJSON());return a},redraw:function(){var a=gmap._.extend({},this.unselected_poly_options);this._highlighted?a=gmap._.extend(a,this._highlighted_poly_options):this._selected&&(a=gmap._.extend(a,this._selected_poly_options));for(i=0,len=this.polygons.length;i<len;i++)this.polygons[i].setOptions(a)},getSelected:function(){return this._selected},
+setSelected:function(a){if(a===true)this.controller.selected!==null&&this.controller.selected.setSelected(false),this._selected=true,this.redraw(),this.controller.selected=this,this.selectCallback&&this.selectCallback();else if(a===false)this._selected=false,this.redraw()},getHighlighted:function(){return this._highlighted},setHighlighted:function(a){if(a===true&&this._highlighted===false)this._highlighted=true,this.redraw(),this.highlightCallback&&this.highlightCallback();else if(a===false&&this._highlighted===
+true)this._highlighted=false,this.redraw()}}})();if(!window.typeOf)window.typeOf=function(a){var b=typeof a;b==="object"&&(a?a instanceof Array&&(b="array"):b="null");return b};if(!window.console)window.console={log:function(){}};gmap=gmap||{};gmap.geom=gmap.geom||{};
+gmap.geom.ParseGeoJSONMultiPolygon=function(a){var b,g,c,e,d,f,h=[],k,j;for(b=0,g=a.length;b<g;b++){k=[];for(c=0,e=a[b].length;c<e;c++){j=[];for(d=0,f=a[b][c].length;d<f;d++)j.push(new google.maps.LatLng(a[b][c][d][1],a[b][c][d][0]));k.push(j)}h.push(k)}return h};gmap.geom.ParseGeoJSONPolygon=function(a){var b,g,c,e,d=[],f;for(b=0,g=a.length;b<g;b++){f=[];for(c=0,e=a[b].length;c<e;c++)f.push(new google.maps.LatLng(a[b][c][1],a[b][c][0]));d.push(f)}return d};
gmap.geom.ParseKMLMultiPolygon=function(a){return $.map($(a).find("Polygon"),function(a){return[$.map($(a).find("coordinates"),function(a){a=$(a).text().split(/\s+/);return[$.map(a,function(a){if(a!=="")return new google.maps.LatLng(parseFloat($.trim(a).split(",")[1]),parseFloat($.trim(a).split(",")[0]))})]})]})};gmap=gmap||{};
(function(){function a(a){a=$(a);return $.map(a.find("Placemark"),function(a){var b={};$placemark=$(a);b.geometry=$placemark.find("MultiGeometry")[0];b.id=$placemark.find("name").text();b.properties={};for(var a=$placemark.find("ExtendedData Data"),e,d,f=0,h=a.length;f<h;f++)e=$(a[f]),d=e.find("value").text(),isNaN(parseFloat(d))||(d=Number(d)),b.properties[e.attr("name")]=d;return b})}gmap.load_polygons=function(b){var g={},c=b.data,e={selected:null},c=b.data_type=="kml"?a(c):c.features;b.unselected_opts&&
gmap._.extend(gmap.Feature.prototype._unselected_poly_options,b.unselected_opts);b.highlighted_opts&&gmap._.extend(gmap.Feature.prototype._highlighted_poly_options,b.highlighted_opts);b.selected_opts&&gmap._.extend(gmap.Feature.prototype._selected_poly_options,b.selected_opts);for(var d,f=0,h=c.length;f<h;f++){d=typeOf(c[f].geometry.coordinates)!=="array"?gmap.geom.ParseKMLMultiPolygon(c[f].geometry):c[f].geometry.type=="Polygon"?[gmap.geom.ParseGeoJSONPolygon(c[f].geometry.coordinates)]:gmap.geom.ParseGeoJSONMultiPolygon(c[f].geometry.coordinates);
View
50 src/feature.js
@@ -171,24 +171,38 @@ var gmap = gmap || {};
}
return multipoly;
},
+ // Redraw the polygons associated with the feature
+ redraw: function() {
+ var opts = gmap._.extend({}, this.unselected_poly_options);
+
+ if(this._highlighted) {
+ opts = gmap._.extend(opts, this._highlighted_poly_options);
+ } else if(this._selected) {
+ opts = gmap._.extend(opts, this._selected_poly_options);
+ }
+
+ for (i=0,len=this.polygons.length; i<len; i++) {
+ this.polygons[i].setOptions(opts);
+ }
+ },
getSelected: function() {
return this._selected;
},
setSelected: function(value) {
var i, len;
if (value === true) {
- if (this.controller.selected !== null) { this.controller.selected.setSelected(false); }
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this._selected_poly_options);
- }
+ if (this.controller.selected !== null) {
+ this.controller.selected.setSelected(false);
+ }
this._selected = true;
- this.controller.selected = this;
- if (this.selectCallback) { this.selectCallback(); }
+ this.redraw();
+ this.controller.selected = this;
+ if (this.selectCallback) {
+ this.selectCallback();
+ }
} else if (value === false) {
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this.unselected_poly_options);
- }
this._selected = false;
+ this.redraw();
}
},
getHighlighted: function() {
@@ -198,21 +212,13 @@ var gmap = gmap || {};
var i,len;
if ((value === true) && (this._highlighted === false)) {
this._highlighted = true;
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(this._highlighted_poly_options);
- }
- if (this.highlightCallback) { this.highlightCallback(); }
+ this.redraw();
+ if (this.highlightCallback) {
+ this.highlightCallback();
+ }
} else if ((value === false) && (this._highlighted === true)) {
this._highlighted = false;
- var opts;
- if (this.getSelected()) {
- opts = gmap._.extend({}, this.unselected_poly_options, this._selected_poly_options);
- } else {
- opts = gmap._.extend({}, opts, this.unselected_poly_options);
- }
- for (i=0,len=this.polygons.length; i<len; i++) {
- this.polygons[i].setOptions(opts);
- }
+ this.redraw();
}
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.