var _locator = null; var _searchType = null; var _addressLOD = null; function initializeGeoCoder() { _locator = new esri.tasks.Locator(ESRIConfig.locatorURL); dojo.connect(_locator, "onAddressToLocationsComplete", onAddressToLocationsCompleteHandler); } function locateByAddress(address, city, state, zip, lod) { _searchType = null; if (address && zip){ _searchType = ESRIConfig.searchTypeAddress; } else if(address && city) { _searchType = ESRIConfig.searchTypeAddress; } else { if (city && state) { _searchType = ESRIConfig.searchTypeCityState; } else { if (zip) { _searchType = ESRIConfig.searchTypeZip; }else if(city){ queryCityLayer(city, state); return; }else if (state) { _searchType = ESRIConfig.searchTypeState; } else{ updateMessage("error.address.null.fields"); return; } } } if(lod != null){ _addressLOD = lod; } else{ _addressLOD = ESRIDefaults.searchTypeLODs[_searchType]; } clearMessage(); if (_searchType == ESRIConfig.searchTypeState) { var latLongLOD = ESRIDefaults.stateLatLongLODs[state.toUpperCase()]; if (latLongLOD != null ) { latLongLOD = latLongLOD.split(','); if(latLongLOD.length == 3){ locateByLatLong(latLongLOD[0], latLongLOD[1], latLongLOD[2], false); } else{ queryStateLayer(state); } } else { queryStateLayer(state); } } else { //ignore states that only have 1 letter if(state!= null){ if(state.length < 2){ state = ""; } } var location = { Address : address, City : city, State : state, Zip : zip }; _locator.addressToLocations(location, [ "Loc_name" ]); } } function locateByLatLong(lat, lng, lod, showSymbol) { if (lod == null) { lod = ESRIDefaults.searchTypeLODs[ESRIConfig.searchLatLong]; } if (showSymbol == null) { showSymbol = true; } if (validateLatLong(lat, lng)) { centerMap(lng, lat, lod, showSymbol); } } function queryDataLayers(){ var city = dojo.byId(UIConfig.cityInputID).value; var state = dojo.byId(UIConfig.stateInputID).value; var zip = dojo.byId(UIConfig.zipInputID).value; if(zip !=""){ if(_searchType == ESRIConfig.searchTypeZip){ if(zip.length > 5){ zip = zip.substr(0,5); } if(zip.length == 5){ zip = zip.substr(0,4); } } queryZipLayer(zip); } else if(city != ""){ queryCityLayer(city, state); } else if(state !=""){ queryStateLayer(state); } else{ updateMessage("error.address.no.matches"); } } function queryCityLayer(city, state){ if(state == null){ state = ""; } state = state.toUpperCase(); city = capitalizeFirstLetter(city); var outFields = [ "STATE","POPULATION"]; var where = ""; if(state.length == 0){ where = "NAME LIKE '"+ city +"%' "; } else if(state.length == 1 || state.length == 2){ where = "NAME LIKE '"+ city +"%' AND STATE LIKE '"+state+"%'"; } else if(state.length > 2){ where = where = "NAME LIKE '"+ city +"%' AND ST_NAME LIKE '"+state+"%'"; } else{ where = "NAME LIKE '"+ city +"%' "; } var query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = outFields; query.where = where; var queryTask = new esri.tasks.QueryTask(ESRIConfig.cityLayer); queryTask.execute(query,cityLayerResults); } function queryStateLayer(state){ state = state.toUpperCase(); var outFields = [ "STATE","STATE_NAME"]; var where = ""; if(state.length < 3){ where = "STATE LIKE '"+ state +"%'"; } else{ where = "STATE_NAME LIKE '"+ state +"%'"; } var query = new esri.tasks.Query(); query.returnGeometry = true; query.outFields = outFields; query.where = where; var queryTask = new esri.tasks.QueryTask(ESRIConfig.stateLayer); queryTask.execute(query, stateLayerResults); } function queryZipLayer(zip){ var where = "POSTAL LIKE '"+ zip +"%'"; var outFields = ["POSTAL", "STATE", "NAME", "LATITUDE", "LONGITUDE"]; var query = new esri.tasks.Query(); query.returnGeometry = false; query.outFields = outFields; query.where = where; var queryTask = new esri.tasks.QueryTask(ESRIConfig.zipLayer); queryTask.execute(query,zipLayerResults); } function validateLatLong(lat, lng) { if (lat == "") { displayMessageLL(message('error.latlong.empty.lat')); return false; } // Protects against a blank longitude value. if (lng == "") { displayMessageLL(message('error.latlong.empty.long')); return false; } // Makes sure the latitude value is a number. if (isNaN(lat)) { displayMessageLL(message('error.latlong.invalid.lat')); return false; } // Makes sure the longitude value is a number. if (isNaN(lng)) { displayMessageLL(message('error.latlong.invalid.long')); return false; } // Performs range checking on the latitude value. if (lat < 15 || lat > 75) { displayMessageLL(message('error.latlong.invalid.range.lat')); return false; } // Performs range checking on the longitude value. if (lng < -180 || lng > -60) { displayMessageLL(message('error.latlong.invalid.range.long')); return false; } clearMessageLL(); return true; } // -------------------------------------------------------------------------- // // Event Handlers // // -------------------------------------------------------------------------- function onAddressToLocationsCompleteHandler(candidates) { var candidate; var candidateList = new Array(); var index = 0; if(candidates!=null){ candidates = removeDuplicateCanidates(candidates); } for ( var i = 0, il = candidates.length; i < il; i++) { candidate = candidates[i]; if (candidate.score > 70 && candidate.attributes.Loc_name == _searchType) { candidateList[index++] = candidate; if (candidate.score >= 100 || index >= ESRIDefaults.numberCandidatesReturned) { if (candidate.score >= 100) { candidateList = new Array(); candidateList[0] = candidate; } break; } } } if (candidateList.length <= 0) { if(_searchType == ESRIConfig.searchTypeAddress){ updateMessage("error.address.no.matches"); }else{ queryDataLayers(); } } else { if (candidateList.length > 1) { var innerHtml = ""; if (UIConfig.addressListTable != null && UIConfig.addressListTable.length > 0) { innerHtml = UIConfig.addressListTable; } innerHtml += ""; for ( var i = 0; i < candidateList.length; ++i) { innerHtml += formatAddress(candidateList[i].address, candidateList[i].location.x, candidateList[i].location.y); } innerHtml += "
"; updateMessage("error.address.multiple.matches", innerHtml); } else { updateMessage(); centerMap(candidateList[0].location.x, candidateList[0].location.y, _addressLOD, true); _addressLOD = null; } } } function cityLayerResults(results){ var lod = ESRIDefaults.searchTypeLODs[ESRIConfig.searchTypeCityState]; var cities = results.features; cities.sort(sortByPopulation); if(cities.length == 0){ updateMessage("error.address.no.matches"); return; } else if(cities.length == 1){ dojo.byId(UIConfig.cityInputID).value = cities[0].attributes.NAME; dojo.byId(UIConfig.stateInputID).value = cities[0].attributes.STATE; dojo.byId(UIConfig.zipInputID).value = ""; dojo.byId(UIConfig.addressInputID).value = ""; var x = cities[0].geometry.x; var y = cities[0].geometry.y; centerMap(x, y, lod, true); clearMessage(); } else{ var innerHtml = ""; if (UIConfig.addressListTable != null && UIConfig.addressListTable.length > 0) { innerHtml = UIConfig.addressListTable; } innerHtml += ""; var loopCount = Math.min(ESRIDefaults.numberCandidatesReturned, cities.length); for ( var i = 0; i < loopCount; ++i) { var locationText = cities[i].attributes.NAME +"- "; locationText += cities[i].attributes.STATE +" "; var x = cities[i].geometry.x; var y = cities[i].geometry.y; innerHtml += formatAddress(locationText, x, y, lod); } innerHtml += "
"; updateMessage("error.address.multiple.matches", innerHtml); } } function stateLayerResults(results){ var lod = ESRIDefaults.searchTypeLODs[ESRIConfig.searchTypeState]; var states = results.features; if(states.length == 0){ updateMessage("error.address.no.matches"); return; } else if(states.length == 1){ dojo.byId(UIConfig.cityInputID).value = ""; dojo.byId(UIConfig.stateInputID).value = states[0].attributes.STATE; dojo.byId(UIConfig.zipInputID).value = ""; dojo.byId(UIConfig.addressInputID).value = ""; var x = states[0].geometry.x; var y = states[0].geometry.y; centerMap(x, y, lod, true); clearMessage(); } else{ var innerHtml = ""; if (UIConfig.addressListTable != null && UIConfig.addressListTable.length > 0) { innerHtml = UIConfig.addressListTable; } innerHtml += ""; var loopCount = Math.min(ESRIDefaults.numberCandidatesReturned, states.length); for ( var i = 0; i < loopCount; ++i) { var locationText = states[i].attributes.STATE_NAME +" "; var x = states[i].geometry.x; var y = states[i].geometry.y; innerHtml += formatAddress(locationText, x, y, lod); } innerHtml += "
"; updateMessage("error.address.multiple.matches", innerHtml); } } function zipLayerResults(results){ var lod = ESRIDefaults.searchTypeLODs[ESRIConfig.searchTypeZip]; if(results.features.length == 0){ updateMessage("error.address.no.matches"); return; } else if(results.features.length == 1){ dojo.byId(UIConfig.addressInputID).value = ""; dojo.byId(UIConfig.cityInputID).value = results.features[0].attributes.NAME; dojo.byId(UIConfig.stateInputID).value = results.features[0].attributes.STATE; dojo.byId(UIConfig.zipInputID).value = results.features[0].attributes.POSTAL; var x = results.features[i].attributes.LONGITUDE; var y = results.features[i].attributes.LATITUDE; centerMap(x, y, lod, true); clearMessage(); } else{ var innerHtml = ""; if (UIConfig.addressListTable != null && UIConfig.addressListTable.length > 0) { innerHtml = UIConfig.addressListTable; } innerHtml += ""; var loopCount = Math.min(ESRIDefaults.numberCandidatesReturned, results.features.length); for ( var i = 0; i < loopCount; ++i) { var locationText = results.features[i].attributes.POSTAL +","; locationText += results.features[i].attributes.NAME +"- "; locationText += results.features[i].attributes.STATE +" "; var x = results.features[i].attributes.LONGITUDE; var y = results.features[i].attributes.LATITUDE; innerHtml += formatAddress(locationText, x, y, lod); } innerHtml += "
"; updateMessage("error.address.multiple.matches", innerHtml); } } //-------------------------------------------------------------------------- // // Utility Functions // // -------------------------------------------------------------------------- function capitalizeFirstLetter(text) { capText = ''; words = text.split(' '); for(var c=0; c < words.length; c++) { capText += words[c].substring(0,1).toUpperCase() + words[c].substring(1,words[c].length).toLowerCase() + ' '; } capText += "$"; return capText.replace(' $',''); } function formatAddress(address, x, y, lod) { // Basic address has a linbreak between first element (i.e. street) // and rest of text. The second comma (presumably between city & state) // is also removed address = address.replace(",", "
"); address = address.replace(",", " "); address = address.replace("-", ","); // A default zoom scale of 9 is assumed. Lookup the zoom scale based on // the type of search being done. if(lod == null){ var lod = 9; if (ESRIDefaults.searchTypeLODs != null && _searchType != null && ESRIDefaults.searchTypeLODs[_searchType]) { lod = ESRIDefaults.searchTypeLODs[_searchType]; } } // The reference to zoom the map to href = ""; if (UIConfig.addressTableRowHTML != null) { // Custom address display. // Use [ADDRESS] (case sensitive) as a place holder for // the location of the address. // Use = 0) { result = result.replace(""; } } return result; } function removeDuplicateCanidates(canidates){ if(canidates == null){ return null; } else if(canidates.length >0){ var points = [canidates[0].location]; var uniqueCanidates = [canidates[0]]; for (i=1; i y){ return -1; } else{ return 0; } }