");
$('head').append("");
initEsriMap("esrimap", false, true);
// this is to fix the responsiveness of columns
$('.tab-column[style*="width:50%"]').addClass('col-xs-12 col-sm-12 col-md-6 col-lg-6').css('width', "");
}
});
function loadMap(sectionId, MapSettings, point) {
require([
"esri/tasks/Locator",
"esri/Map",
"esri/views/MapView",
"esri/layers/Layer",
"esri/geometry/Point",
"esri/symbols/PictureMarkerSymbol",
"esri/symbols/SimpleMarkerSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/TextSymbol",
"esri/symbols/Font",
"esri/Graphic",
"esri/layers/GraphicsLayer",
"esri/widgets/Locate",
"esri/widgets/Search",
"esri/layers/FeatureLayer",
"esri/geometry/Polygon",
"esri/geometry/SpatialReference",
"dojo/dom",
"dojo/number",
"dojo/on",
"dojo/domReady!"
],
function (
Locator, Map, MapView, Layer, Point, PictureMarkerSymbol, SimpleMarkerSymbol, SimpleFillSymbol, TextSymbol, Font, Graphic, GraphicsLayer, Locate, Search, FeatureLayer, Polygon, SpatialReference,
dom, number, on) {
//console.log("MapSettings", MapSettings);
var rangepoints = [];
var spatialReference = new SpatialReference({
wkid: MapSettings.map_abs_spatialreference
});
// Set up a locator task using the world geocoding service
var locatorTask = new Locator({
url: MapSettings.server_abs_geocoderesturl
});
// Create the Map
var map = new Map({
basemap: MapSettings.map_abs_basemap
});
// Create the MapView
var view = new MapView({
container: sectionId,
map: map,
center: new Point(MapSettings.map_abs_longitude, MapSettings.map_abs_latitude, new SpatialReference({
wkid: MapSettings.map_abs_spatialreference
})),
zoom: MapSettings.map_abs_initialzoom
});
var pinSymbol;
if (MapSettings.map_abs_pushpinimage != null) {
pinSymbol = new PictureMarkerSymbol(MapSettings.map_abs_pushpinimage, MapSettings.map_abs_pushpinheight, MapSettings.map_abs_pushpinwidth);
} else {
pinSymbol = new SimpleMarkerSymbol({
style: "circle",
color: "#337ab7",
size: MapSettings.map_abs_pushpinheight + "px" // pixels
});
}
var boundaryPolygon;
if (MapSettings.map_abs_polygonboundary != null) {
boundaryPolygon = new Polygon({
hasZ: false,
hasM: false,
rings: MapSettings.map_abs_polygonboundary,
spatialReference: spatialReference
});
}
/******************************************************
Locate Widget logic
********************************************/
var locateBtn = new Locate({
viewModel: { // autocasts as new LocateViewModel()
view: view, // assigns the locate widget to a view
goToLocationEnabled: false
}
});
if (MapSettings.map_abs_enablelocate) {
// Add the locate widget to the top left corner of the view
view.ui.add(locateBtn, {
position: "top-left"
});
}
locateBtn.on("locate", function (data) {
var lat = data.position.coords.latitude;
var lon = data.position.coords.longitude;
// check polygon boundary before making reverseGeocode call.
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(lon, lat))) {
showMessage('Your current location is outside of the polygon boundary.');
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
reverseGeocode({
X: lon,
Y: lat
}, function (data) {
//console.log(data);
map.layers.removeAll();
view.graphics.removeAll();
if (typeof data != 'object')
data=JSON.parse(data);
if (data.hasOwnProperty("error")) {
plotPointOnMap(data.point, "[" + data.point.X + ", " + data.point.Y + "]");
setCrmFormValues(null, data.point.X, data.point.Y);
} else {
if (inAddressBoundaries(data.address, true)) {
plotPointOnMap({
X: data.location.x,
Y: data.location.y
}, data.address.Address);
setCrmFormValues(data.address, data.location.x, data.location.y);
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
}
});
});
locateBtn.on("locate-error", function () {
showMessage('Unable to find your current location.');
});
/******************************************************
Search Widget logic
********************************************/
if (!MapSettings.map_abs_uselocationfieldforsearch) {
var searchWidget = new Search({
view: view,
popupEnabled: false,
resultGraphicEnabled: false,
autoNavigate: false,
searchQueryParams: {
outSpatialReference: MapSettings.map_abs_spatialreference
},
});
if (MapSettings.map_abs_enablesearch) {
// Add the search widget to the very top left corner of the view
view.ui.add(searchWidget, {
position: "top-left",
index: 0
});
}
searchWidget.on("select-result", function (event) {
var x = event.result.feature.geometry.x;
var y = event.result.feature.geometry.y;
var lat = event.result.feature.geometry.latitude;
var lon = event.result.feature.geometry.longitude;
// check polygon boundary before making reverseGeocode call.
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(lon, lat))) {
showMessage("Selected address does not fall within the boundary;");
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
if (MapSettings.map_abs_enableBoundaryOnSearch) {
GeocodeAddress(event.result.name, true, function (data) {
//console.log(data);
data.candidates[0].attributes['Address'] = data.candidates[0].address.split(",")[0];
data.candidates[0].attributes['CountryCode'] = data.candidates[0].attributes.Country;
// console.log(data.candidates[0]);
if (inAddressBoundaries(data.candidates[0].attributes)) {
plotPointOnMap({
X: data.candidates[0].location.x,
Y: data.candidates[0].location.y
}, data.candidates[0].attributes.Address);
setCrmFormValues(data.candidates[0].attributes, data.candidates[0].location.x, data.candidates[0].location.y)
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
}, 1, false);
} else {
plotPointOnMap({
X: lon,
Y: lat,
latitude: lat,
longitude: lon
}, event.result.name.split(',')[0]);
}
});
} else {
$('body').append('');
var search = new Search({
view: view,
popupEnabled: false,
resultGraphicEnabled: false,
autoNavigate: false,
searchQueryParams: {
outSpatialReference: MapSettings.map_abs_spatialreference
},
container: MapSettings.mapping_abs_locationfieldname
});
search.on("select-result", function (event) {
var x = event.result.feature.geometry.x;
var y = event.result.feature.geometry.y;
var lat = event.result.feature.geometry.latitude;
var lon = event.result.feature.geometry.longitude;
// check polygon boundary before making reverseGeocode call.
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(lon, lat))) {
showMessage("Selected address does not fall within the boundary;");
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
if (MapSettings.map_abs_enableBoundaryOnSearch) {
GeocodeAddress(event.result.name, true, function (data) {
// console.log(data);
data.candidates[0].attributes['Address'] = data.candidates[0].address.split(",")[0];
data.candidates[0].attributes['CountryCode'] = data.candidates[0].attributes.Country;
// console.log(data.candidates[0]);
if (inAddressBoundaries(data.candidates[0].attributes)) {
plotPointOnMap({
X: data.candidates[0].location.x,
Y: data.candidates[0].location.y
}, data.candidates[0].attributes.Address);
setCrmFormValues(data.candidates[0].attributes, data.candidates[0].location.x, data.candidates[0].location.y)
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
}, 1, false);
} else {
plotPointOnMap({
X: lon,
Y: lat,
latitude: lat,
longitude: lon
}, event.result.name.split(',')[0]);
}
});
}
/*******************************************************************
* This click event .....
*******************************************************************/
if (MapSettings.map_abs_enableclickreversegeocode) {
view.on("click", function (event) {
if ($("#clickToggle").hasClass('on')) {
// hides graphic from locateBtn
view.graphics.removeAll();
// Get the coordinates of the click on the view
var lat = event.mapPoint.latitude;
var lon = event.mapPoint.longitude;
// check polygon boundary before making reverseGeocode call.
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(lon, lat))) {
showMessage("Please plot point within the boundary.");
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
var clickmode = $("#ReverseGeolocationType button.active").attr('data-type');
if (clickmode != "range") {
reverseGeocode({
X: lon,
Y: lat
}, function (data) {
// console.log(data);
if(typeof data != 'object')
data=JSON.parse(data);
if (data.hasOwnProperty("error")) {
plotPointOnMap(data.point, "[" + data.point.X + ", " + data.point.Y + "]");
setCrmFormValues(null, data.point.X, data.point.Y);
} else {
if (inAddressBoundaries(data.address)) {
plotPointOnMap({
X: data.location.x,
Y: data.location.y
}, data.address.Address);
//console.log(data.address);
setCrmFormValues(data.address, data.location.x, data.location.y);
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
}
});
} else {
var point = new Array(lon, lat);
//console.log('points', point);
rangepoints.push(point);
plotPointOnMap({
X: lon,
Y: lat
}, rangepoints.length);
// console.log('onclick ragepoints', rangepoints)
}
}
});
} else {
// remove toggle button
$("#clickToggle").remove();
}
/*******************************************************************
* On map loaded .....
*******************************************************************/
view.then(function () {
// This function will execute once the promise is resolved
addCustomControl();
if (!window.AdoxioData) {
window.AdoxioData = {};
}
if (MapSettings.map_abs_uselocationfieldforsearch) {
$("#" + MapSettings.mapping_abs_locationfieldname + "_input").addClass("form-control text")
}
// map appearance settings
if (!MapSettings.map_abs_showzoombuttons) {
view.ui.remove("zoom");
}
if (!MapSettings.map_abs_showattribution) {
view.ui.remove("attribution");
} else {
$("#ReverseGeolocationType, #clickToggle").addClass('showAttribution');
}
view.ui.padding = 5;
if (!MapSettings.map_abs_allowintersectionaddress) {
$("#ReverseGeolocationType button[data-type='intersection']").remove();
}
if (!MapSettings.map_abs_allowrangeaddress) {
$("#ReverseGeolocationType button[data-type='range']").remove();
}
// attach events
$("#ReverseGeolocationType #clear").on('click', function () {
if (map.layers != null) {
map.layers.removeAll();
}
rangepoints = [];
});
$("#ReverseGeolocationType #done").on('click', function () {
if (rangepoints != null) {
// we will store the center of the polygon in lat and long fields
//console.log("polygon", polygon.centroid);
setCrmFormValues(null, rangepoints[0][0], rangepoints[0][1], "range");
drawPolygonOnMap();
}
});
try {
//console.log('token logic');
// Token logic
if (MapSettings.server_abs_enabletoken) {
if (MapSettings.server_abs_applicationtoken == null || MapSettings.server_abs_tokenexpiry == null) {
getToken();
} else {
if (moment().isBefore(moment(MapSettings.server_abs_tokenexpiry))) {
console.log('token is valid');
} else {
getToken();
}
}
}
} catch (err) {
console.log(err);
}
// make address required
try {
if ($("#" + MapSettings.mapping_abs_locationfieldname).length > 0) {
addRequiredValidators("#" + MapSettings.mapping_abs_locationfieldname);
}
} catch (er) {
console.log(er);
}
// plot existing point on record
var lat,
lon,
address = null;
if ($("#" + MapSettings.mapping_abs_longitudefieldname) != null)
lon = $("#" + MapSettings.mapping_abs_longitudefieldname).val();
if ($("#" + MapSettings.mapping_abs_latitudefieldname) != null)
lat = $("#" + MapSettings.mapping_abs_latitudefieldname).val();
if ($("#" + MapSettings.mapping_abs_locationfieldname) != null)
address = $("#" + MapSettings.mapping_abs_locationfieldname).val();
if (lon && lat) {
plotPointOnMap({
X: lon,
Y: lat
}, address);
} else if (/\S/.test(address) && address != null) {
if (address.indexOf('Point on Map') > -1) {
var pointString = address.substring(address.lastIndexOf("[") + 1, address.lastIndexOf("]"));
var points = pointString.split(',');
// console.log(points);
plotPointOnMap({
X: points[1],
Y: points[0]
}, "[" + pointString + "]");
} else if (address.indexOf('Area on Map') > -1) {
// console.log('in here should only be on load');
var pointsArray = address.substring(address.lastIndexOf("[") + 1, address.lastIndexOf("]")).split(',');
// console.log('pointsArray', pointsArray);
var polygonRings = [];
for (i = 0; i < pointsArray.length; i++) {
var point = [parseFloat(pointsArray[i]), parseFloat(pointsArray[i + 1])];
i++;
polygonRings.push(point);
}
// console.log(polygonRings);
//var pointsArray = split2DArray(pointsString,"|",",");
rangepoints = polygonRings;
drawPolygonOnMap();
} else {
GeocodeAddress(address, false, function (data) {
plotPointOnMap({
X: data.candidates[0].location.x,
Y: data.candidates[0].location.y
}, data.candidates[0].address.split(',')[0]);
// console.log('geocode', data.candidates[0]);
}, 1, false);
}
} else {
if (MapSettings.map_abs_geolocationonload) {
if (location.protocol == 'https:') {
// console.log("locate me!");
locateBtn.locate();
} else {
showMessage("Geolocation is only supported on secure(HTTPS) websites.");
}
} else {
view.goTo([MapSettings.map_abs_longitude, MapSettings.map_abs_latitude]);
}
}
// attach field activations
if (MapSettings.mapping_abs_geocodeactivationfieldnames != null) {
$(MapSettings.mapping_abs_geocodeactivationfieldnames.split(',')).each(function (index, fieldname) {
if ($("#" + fieldname) != null) {
var eventType = "change";
if (endsWith(fieldname, "composite")) {
fieldname = $("#" + fieldname).parent().find(".btn").attr('id'); // popover done button id
eventType = "click";
}
$('body').on(eventType, "#" + fieldname, function (event) {
// console.log(eventType, fieldname);
var address = GetAddressFromCRMForm();
GeocodeAddress(address, true, function (data) {
// console.log(data);
if (data.candidates[0].score == 100) {
data.candidates[0].attributes['Address'] = data.candidates[0].address.split(",")[0];
data.candidates[0].attributes['CountryCode'] = data.candidates[0].attributes.Country;
// check polygon boundary is valid
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(data.candidates[0].location.x, data.candidates[0].location.y))) {
showMessage("Please type an address within the boundary.");
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
if (inAddressBoundaries(data.candidates[0].attributes)) {
plotPointOnMap({
X: data.candidates[0].location.x,
Y: data.candidates[0].location.y
}, data.candidates[0].attributes.Address);
setCrmFormValues(data.candidates[0].attributes, data.candidates[0].location.x, data.candidates[0].location.y)
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
} else {
// we show a modal with candidates
// save them for access in callback
window.AdoxioData.ModalOptions = data.candidates;
var modalcontrols = "";
$(data.candidates).each(function (index, item) {
var radio = '
We have found multiple matching addresses to your location. Please select the correct or closest one.
", modalcontrols, function (data) {
var result = $('#ESRIModal input[name="address"]:checked').val() || null;
if (result == -1 || data == 'false') {
if (!MapSettings.map_abs_allowunvalidatedaddress) {
setCrmFormValues(null, null, null);
}
$("#ESRIModal").modal('hide');
//$("#ESRIModal").remove();
return;
}
var choice = window.AdoxioData.ModalOptions[result];
// console.log('we have selected:', choice);
window.AdoxioData.ModalOptions = {};
choice.attributes['Address'] = choice.address.split(",")[0];
choice.attributes['CountryCode'] = choice.attributes.Country;
// check polygon boundary is valid
if (MapSettings.map_abs_polygonboundary != null) {
if (!boundaryPolygon.contains(new Point(choice.location.x, choice.location.y))) {
showMessage("Please type an address within the boundary.");
var polygonCenter = boundaryPolygon.centroid;
view.goTo({
center: polygonCenter,
zoom: MapSettings.map_abs_initialzoom
});
return;
}
}
if (inAddressBoundaries(choice.attributes)) {
plotPointOnMap({
X: choice.location.x,
Y: choice.location.y
}, choice.attributes.Address);
setCrmFormValues(choice.attributes, choice.location.x, choice.location.y);
} else {
// selected point is not in boundary take user back to center of initial point and zoom
view.goTo({
center: [MapSettings.map_abs_longitude, MapSettings.map_abs_latitude],
zoom: MapSettings.map_abs_initialzoom
});
}
$("#ESRIModal").modal('hide');
//$("#ESRIModal").remove();
});
}
});
});
}
});
}
}, function (error) {
// This function will execute if the promise is rejected due to an error
});
/*******************************************************************
*Functions
*******************************************************************/
var addCustomControl = function () {
var css = '';
$('body').append(css);
var controls = '