-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeozip.js
More file actions
181 lines (147 loc) · 4.94 KB
/
Copy pathgeozip.js
File metadata and controls
181 lines (147 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
!function(definition) {
window.geoZip = definition(jQuery);
}(function($) {
var map, toolbar, symbol, geomTask
, Map, Draw, Graphic, Color, SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol, TextSymbol, Font, parser, registry, Query, QueryTask, Extent, SpatialReference, on, query, all,connect
, ZIP_SERVICE = "http://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_ZIP_Codes/FeatureServer/0"
, CHAR = 65 // ASCII "A"
, GEOMETRIES = {}
;
// the Geometry object
function Geometry(label,evt) {
var symbol = evt.geometry.type=="point" ? null : new SimpleFillSymbol()
, midpoint = evt.geometry.type=="point" ? evt.geometry : evt.geometry.getExtent().getCenter()
, textSymbol = new TextSymbol(label)
.setColor(new Color([255,0,0]))
.setAlign(Font.ALIGN_START)
.setFont(new Font("14pt").setWeight(Font.WEIGHT_BOLD))
;
toolbar.deactivate();
map.showZoomSlider();
this.label = label;
this.data = [];
this.symbol = new Graphic(evt.geometry, symbol);
this.text = new Graphic(midpoint, textSymbol)
map.graphics.add(this.symbol);
map.graphics.add(this.text);
this.getZipCodes();
}
Geometry.prototype.getZipCodes = function() {
var qTask = new QueryTask(ZIP_SERVICE)
, q = new Query()
, self = this
;
// specify proxy for request with URL lengths > 2k characters
esriConfig.defaults.io.proxyUrl = "/proxy/";
q.returnGeometry = false;
q.outFields = ["ZIP","PO_NAME","STATE"];
q.geometry = this.symbol.geometry;
//qTask.execute(q, showResults);
qTask.execute(q, function(results) {
results.features.forEach(function(z) {
var attr = z.attributes;
self.data.push([attr.ZIP, attr.PO_NAME, attr.STATE]);
});
var $widget = self.html().appendTo('#results');
$widget.on('click','input[value=Remove]', function() {
var label = $(this).closest('.mk-geometry').data('label');
geoZip.removeGeometry(label);
});
});
}
Geometry.prototype.html = function() {
var html = [
'<div class="mk-geometry" data-label="'+this.label+'">',
' <h3>Geometry-'+this.label+' <input type="button" value="Remove"/></h3>',
' <table/>',
'</div>'
]
, $widget = $(html.join("\n"))
, $table = $widget.find('table')
;
this.data.forEach(function(z) {
$('<tr><td>'+z[0]+'</td><td>'+z[1]+'</td><td>'+z[2]+'</td></tr>').appendTo($table);
});
return $widget;
}
// other utilities
function activateTool() {
var tool = this.label.toUpperCase().replace(/ /g, "_");
toolbar.activate(Draw[tool]);
map.hideZoomSlider();
}
function createToolbar(themap) {
toolbar = new Draw(map);
toolbar.on("draw-end", addToMap);
}
function addToMap(evt) {
var label = String.fromCharCode(CHAR++);
GEOMETRIES[label] = new Geometry(label,evt);
}
pub = {
init : function(xMap, xDraw, xGraphic, xColor, xSimpleMarkerSymbol, xSimpleLineSymbol, xSimpleFillSymbol, xTextSymbol, xFont, xQuery, xQueryTask, xExtent, xSpatialReference,
xparser, xregistry, xon, xquery, xall,xconnect) {
// turn arguments into global variables for the geoZip namespace
Map = xMap;
Draw = xDraw;
Graphic = xGraphic;
Color = xColor;
SimpleMarkerSymbol = xSimpleMarkerSymbol;
SimpleLineSymbol = xSimpleLineSymbol;
SimpleFillSymbol = xSimpleFillSymbol;
TextSymbol = xTextSymbol;
Font = xFont;
parser = xparser;
registry = xregistry;
Query = xQuery;
QueryTask = xQueryTask;
Extent = xExtent;
SpatialReference = xSpatialReference,
on = xon;
query = xquery;
all = xall;
connect = xconnect;
parser.parse();
map = new Map("map", {
basemap: "streets",
//center: [-15.469, 36.428],
center:[-98.877,39.814], // middle of US
zoom: 4
});
map.on("load", createToolbar);
// loop through all dijits, connect onClick event
// listeners for buttons to activate drawing tools
registry.forEach(function(d) {
// d is a reference to a dijit
// could be a layout container or a button
if ( d.declaredClass === "dijit.form.Button" ) {
d.on("click", activateTool);
}
});
var self = this;
$('input[value=Submit]').on('click', function() { self.createMailingList(); });
},
removeGeometry : function(label) {
var geom = GEOMETRIES[label];
// remove from the map
map.graphics.remove(geom.text);
map.graphics.remove(geom.symbol);
// remove this Geometry object from GEOMETRIES
delete GEOMETRIES[label];
// remove from sidebar
$('div.mk-geometry[data-label='+label+']').remove();
},
createMailingList : function() {
// build a list of zipcodes from GEOMETRIES
var zips = [];
for ( var g in GEOMETRIES ) {
GEOMETRIES[g].data.forEach(function(z) { zips.push(z[0]);});
//GEOMETRIES[g].data.forEach(function(z) { alert(JSON.stringify(z))});
}
alert(JSON.stringify(zips));
// get a mailing list name
// POST to backend
}
}
return pub;
})