Created
August 7, 2014 21:38
-
-
Save kirkpabk/64ef84acf06933224088 to your computer and use it in GitHub Desktop.
Several rudamentary samples for obtaining bits of information for a Select2 dropdown using AJAX and GIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$("#parcelByAddress.select2").select2({ | |
placeholder: "Select...", | |
minimumInputLength: 2, | |
allowClear: true, | |
autoClear: false, | |
//initSelection: function (element, callback) { | |
// var elementText = element.val(); | |
// data = { id: elementText, text: elementText }; | |
// callback(data); | |
//}, | |
ajax: { | |
quietMillis: 100, /* Your URL will difer */ | |
url: 'http://gisdata.<YourServer>.net/public/rest/services/A_POL_Basemap/MapServer/33/query?geometryType=esriGeometryEnvelope&spatialRel=' + | |
'esriSpatialRelIntersects&relationParam=&outFields=ADDRESS%2CMAP_PARCEL%2COWNER&returnGeometry=false&returnIdsOnly=' + | |
'false&returnCountOnly=false&orderByFields=ADDRESS&returnZ=false&returnM=false&f=pjson', | |
dataType: 'jsonp', /*JSONP required to prevent XMLHttpRequest cannot load "No 'Access-Control-Allow-Origin' error */ | |
data: function (term) { | |
term = "" + term; | |
return 'where=ADDRESS+LIKE+%27%25' + term.toUpperCase() + '%25%27' + | |
'+OR+OWNER+LIKE+%27%25' + term.toUpperCase() + '%25%27' + | |
'+OR+MAP_PARCEL+LIKE+%27%25' + term + '%25%27 ' + | |
'+OR+CONCAT(ADDRESS,OWNER)+LIKE+%27%25' + term.toUpperCase().replace(' ', '%25') + '%25%27 ' | |
}, | |
results: function (data) { | |
var lastStreet = ''; | |
var newJson = { data: [] }; | |
return { results: $.map(data.features, function (item) { | |
var fullStreetName = ""; | |
if (item.attributes.ADDRESS !== null) { fullStreetName += item.attributes.ADDRESS; } | |
if (item.attributes.MAP_PARCEL !== null) { fullStreetName += " [PARCEL: " + item.attributes.MAP_PARCEL + "]"; } | |
if (item.attributes.OWNER !== null) { fullStreetName += " (" + item.attributes.OWNER + ")"; } | |
if (fullStreetName != lastStreet) { | |
lastStreet = fullStreetName; | |
//newJson.data.push( | |
return { | |
id: item.attributes.MAP_PARCEL, | |
text: fullStreetName, | |
}; | |
} | |
})}; | |
} | |
} | |
}); | |
$("#GetZipDistrict").click(function () { | |
$("#GetZipDistrictLabel").attr("hidden", "hidden"); | |
$.ajax({ | |
url: 'http://gisdata.<YourServer>.net/public/rest/services/CompositeAddressLocatorWGS84/GeocodeServer/' + | |
'findAddressCandidates?SingleLine=&outFields=ZIP&outSR=&searchExtent=&f=pjson&Street=' + $("#StreetNumber").val() + | |
'+' + $("#GisStreetName").val().replace(" ", "+") + '&City=<YourCity>&State=&ZIP=', | |
dataType: 'jsonp', | |
success: function (data) { | |
//PARSE ZIP OUT OF RETURNED JSONP | |
var returnedData = data.candidates[0]; //retreive the first (highest percentage) result | |
var geocodeLocation; | |
if (data.candidates.length > 0) { | |
$("input#Zip").val(returnedData.attributes.ZIP); | |
//GET GEOCODED LOCATION | |
geocodeLocation = returnedData.location.x + "%2C" + returnedData.location.y; | |
GetDistrictByGeoCode(geocodeLocation); | |
} | |
else { | |
$("#GetZipDistrictLabel").removeAttr("hidden"); | |
} | |
} | |
}); | |
}); | |
function GetDistrictByGeoCode(geocodeLocation) { | |
$.ajax({ | |
url: 'http://gisdata.<YourServer>.net/public/rest/services/Mosquito/Adulticide_Zones/MapServer/0/query?where=&text=&objectIds=&time=&geometry=' + | |
geocodeLocation + '&geometryType=esriGeometryPoint&inSR=2284&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=DISTRICT&' + | |
'returnGeometry=false&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&' + | |
'groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&f=pjson', | |
dataType: 'jsonp', | |
success: function (data) { | |
//PARSE ZIP OUT OF RETURNED JSONP | |
var returnedData = data.features[0]; | |
var geocodeLocation; | |
if (data.features.length > 0) { | |
//SET DISTRICT FIELD | |
var district = returnedData.attributes.DISTRICT; | |
district = district.toUpperCase(); | |
$("#DistrictId").select2().find("option:contains('" + district + "')").each(function () { | |
$(this).attr("selected", "selected"); | |
}); | |
$("#DistrictId").select2().attr("selected", "selected"); //Required for some reason--won't select correctly otherwise. | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment