EDDM Lite is a JS widget that creates a minimal, domain agnostic EDDM Tool in a given <div>
.
<script src="https://mercury.ourtownamerica.com/eddm-widget/widget.js?key={my API key}"></script>
<div id='eddm'></div>
<script> var eddm = new EDDM('eddm'); </script>
var eddm = new EDDM(options)
Constructor creates the new EDDM object.
Parameterseddm.addLocation(location, radius)
Add a location to the map
ParametersThis method returns a promise which is executed when the location has fully loaded into the UI. eg..
eddm.addLocation("123 main st").then(function(){
alert("location loaded");
});
eddm.addrTypes(addressTypes)
Get or set the address types. Address types are an array containing 'B' for business and 'R' for residential addresses. This array may contain both or just 'R'. 'B' only is not allowed per USPS.
If no parameters are passed to this function it will return the current address types Array, if an Address types array is passed in it will set the setting.
Parameters['B','R']
or ['R']
eddm.noLocs(cb)
Register a function to be called when no locations are found for a given address/location
Parameterseddm.setMapHeight(maxHeight)
Set the maximum height the widget will expand to (in pixels)
Parameterseddm.XSS(cb)
Enable cross site scripting in the callback function, which is provided the window and the document of the iframe the widget is drawn in.
Parameterseddm.routeTypes(routeTypes)
Get or set the carrier route types. Route types are an array which may contain any of the following values: 'city' for city routes, 'rural' for rural routes and/or 'po' for PO boxes.
If no parameters are passed to this function it will return the current route types Array, if a route types array is passed in it will set the setting.
Parameters['city','rural']
eddm.autoSelectRoutes(routes)
Automatically select routes once they are loaded onto the map. This function must be called before the addLocation method is called.
Parameterseddm.getInput()
Get an array of locations loaded in the widget.
eddm.load(callback)
Put a function in queue to be executed when the EDDM Widget has loaded into the iframe
Parameterseddm.getRoutes()
Get an array of the selected routes. The routes in the returned array are represented by an object with the following properties:
eddm.getAllLocations()
Get an array of the the locations loaded in the widget. The locations in the returned array are represented by an object with the following properties:
eddm.removeLocation(ddu)
Remove a location from the widget by DDU
ParametersHere's an example of how to remove a location based on the raw location rather than DDU.
function removeLocationByRawLocation(rawLocation){
var locations = eddm.getAllLocations();
for(var i=locations.length; i--;)
if(locations[i].raw === rawLocation)
eddm.removeLocation(locations[i].ddu);
}
<!DOCTYPE html>
<html>
<head>
<title>EDDM Widget Example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body{
margin: 1em 5em;
}
</style>
</head>
<body>
<center>
<h1>EDDM Widget Example</h1>
<a href="https://mercury.ourtownamerica.com/eddm-widget/docs.html">Click here for documentation</a>
<br><br>
<div><b>Enter a location and radius:</b></div>
<div>
<form id="newlocform">
<input id="newloc" placeholder="Enter a lcoation" style=width:6em; />
<input id="newradi" placeholder="Radius" style='width:2em;' />
<button type=submit>Add Location</button>
</form>
</div>
<div id="eddm-widget" style="width:80%; border:1px solid black; margin:1em auto;"></div>
<button id="getRoutes">Click to get selected routes</button>
</center>
<div id="output"></div>
<script src="https://mercury.ourtownamerica.com/eddm-widget/widget.js?key={MY API KEY}"></script>
<script>
var eddm = false;
// retrieve selected routes info
document.getElementById('getRoutes').onclick = function () {
if(!eddm) return alert("Enter a location and select some routes first.");
var routes = eddm.getRoutes();
// Draw some HTML to show the output
var htmlBuffer = [];
for(var i=routes.length; i--;){
htmlBuffer.push("<ul>");
for(var item in routes[i])
if(routes[i].hasOwnProperty(item) && typeof routes[i][item] === "string")
htmlBuffer.push("<li>"+item+": "+routes[i][item]+"</li>");
htmlBuffer.push("</ul>");
}
document.getElementById('output').innerHTML = htmlBuffer.join('');
};
// handle user inputted locations
document.getElementById('newlocform').onsubmit = function(e){
e.preventDefault();
// If the eddm object has not been instantiated yet...
if(!eddm){
// Instantiate the widget
eddm = new EDDM({id: "eddm-widget"});
// Tell it we want to set residential addresses only to start
eddm.addrTypes(['R']);
}
// Get the location and radius
var locInput=document.getElementById('newloc'),
loc = locInput.value,
radiInput = document.getElementById('newradi'),
radi = radiInput.value;
radi = (radi && radi !== "" && parseFloat(radi)) ? parseFloat(radi) : undefined;
// reset and disable inputs
radiInput.value = "";
locInput.value = "";
radiInput.setAttribute('disabled', true);
locInput.setAttribute('disabled', true);
var buttons = document.getElementsByTagName('button');
for(var i=buttons.length; i--;) buttons[i].setAttribute("disabled", true);
// add the location and then enable inputs again
eddm.addLocation(loc, radi).then(function(){
radiInput.removeAttribute('disabled');
locInput.removeAttribute('disabled');
for(var i=buttons.length; i--;) buttons[i].removeAttribute("disabled");
});
return false;
};
</script>
</body>
</html>