Skip to content

Commit

Permalink
fix(geocode): update client-side geocoding to use arcgis
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Feb 1, 2018
1 parent 5364211 commit 42a81b2
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 29 deletions.
11 changes: 9 additions & 2 deletions client/geocode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ module.exports.suggest = suggest
* Geocode -- returns lat/lng coordinate only
*/

function geocode (address, callback) {
function geocode (address, magicKey, callback) {
let params = ''
if (!callback) {
callback = magicKey
magicKey = undefined
} else {
params = `?magicKey=${magicKey}`
}
log('--> geocoding %s', address)
get('/geocode/' + address, function (err, res) {
get(`/geocode/${address}${params}`, function (err, res) {
if (err) {
log('<-- geocoding error %s', err)
callback(err, res)
Expand Down
18 changes: 4 additions & 14 deletions client/location-suggest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ LocationSuggest.prototype.renderSuggestions = function (input, suggestions) {
suggestions = suggestions.slice(0, 4)

suggestionList.innerHTML = suggestionsTemplate.render({
suggestions: suggestions.map(suggestion => {
suggestion.lat = suggestion.center[1]
suggestion.lon = suggestion.center[0]
return suggestion
})
suggestions
})

each(view.findAll('.suggestion'), function (li) {
Expand Down Expand Up @@ -96,16 +92,10 @@ LocationSuggest.prototype.blurInput = function (e) {
inputGroup.classList.remove('suggestions-open')

var highlight = this.find('.suggestion.highlight')
let coords
let magicKey
if (highlight) {
e.target.value = cleanText(highlight.textContent || '')
coords = {
lat: parseFloat(highlight.getAttribute('data-lat')),
lon: parseFloat(highlight.getAttribute('data-lon'))
}
if (isNaN(coords.lat) || isNaN(coords.lon)) {
coords = undefined
}
magicKey = highlight.getAttribute('data-key')
}

suggestionList.classList.add('empty')
Expand All @@ -116,7 +106,7 @@ LocationSuggest.prototype.blurInput = function (e) {

inputGroup.classList.remove('highlight')

this.locationSelected(e.target, e.target.value, coords)
this.locationSelected(e.target, e.target.value, magicKey)
}

/**
Expand Down
11 changes: 5 additions & 6 deletions client/locations-view/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ View.prototype.currentLocation = function (e) {
}
}

View.prototype.locationSelected = function (target, val, coords) {
this.save(target, val, coords)
View.prototype.locationSelected = function (target, val, magicKey) {
this.save(target, val, magicKey)
}

/**
* Geocode && Save
*/

View.prototype.save = function (el, val, coords) {
View.prototype.save = function (el, val, magicKey) {
var plan = this.model
var name = el.name
val = val || el.value
Expand All @@ -166,11 +166,10 @@ View.prototype.save = function (el, val, coords) {
type: name
})
let locationData = val
if (coords) {
if (magicKey) {
locationData = {
address: val,
lat: coords.lat,
lon: coords.lon
magicKey
}
}
this.model.setAddress(name, locationData, function (err, location) {
Expand Down
14 changes: 14 additions & 0 deletions client/plan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ Plan.prototype.setAddress = function (name, locationData, callback) {
callback(null, location.toJSON())
}
})
} else if (typ(locationData.magicKey) === 'string') {
// received an autcomplete suggestion, do geocode to get details
geocode(address, locationData.magicKey, (err, res) => {
if (err) {
return callback(err)
} else {
location.coordinate(res)
changes[name] = address
changes[name + '_ll'] = res
changes[name + '_valid'] = true
plan.set(changes)
callback(null, location.toJSON())
}
})
} else if (typ(locationData.lat) !== 'number') {
// do regular geocode
geocode(locationData, (err, res) => {
Expand Down
2 changes: 1 addition & 1 deletion configurations/default/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ geocode:
address: 2100 Wilson Blvd, Arlington, VA
lat: 38.891254
lng: -77.085016
searchExtent: -77.5,39.5,-76.5,38.5
searchExtent: -78.4039,38.0784,-76.1902,39.6987

# Default application values
api_url: /api
Expand Down
29 changes: 23 additions & 6 deletions lib/geocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports.suggest = suggest
*/

router.get('/:address', function (req, res) {
encode(req.params.address, function (err, addresses) {
encode(req.params.address, req.query.magicKey, function (err, addresses) {
if (err) {
res.status(400).send(err)
} else {
Expand Down Expand Up @@ -80,7 +80,12 @@ router.get('/suggest/:text', function (req, res) {
* Geocode
*/

function encode (address, callback) {
function encode (address, magicKey, callback) {
if (!callback) {
callback = magicKey
magicKey = undefined
}

if (address.address) {
address = address.address + ', ' + address.city + ', ' + address.state + ' ' +
address.zip
Expand All @@ -91,6 +96,10 @@ function encode (address, callback) {
outFields: '*'
}

if (magicKey) {
options.magicKey = magicKey
}

if (config.geocode.center) {
options.location = config.geocode.center
}
Expand All @@ -104,7 +113,17 @@ function encode (address, callback) {
if (!response.candidates || response.candidates.length === 0) {
callback('Address not found.')
} else {
callback(null, response)
callback(null, response.candidates.map(candidate => {
return {
city: candidate.attributes.City,
coordinate: {
lat: candidate.location.y,
lng: candidate.location.x
},
state: candidate.attributes.Region,
zip: candidate.attributes.Postal
}
}))
}
})
.catch(callback)
Expand Down Expand Up @@ -141,9 +160,7 @@ function reverse (ll, callback) {
*/

function suggest (text, callback) {
const options = {
forStorage: true
}
const options = {}

if (config.geocode.center) {
options.location = config.geocode.center
Expand Down

0 comments on commit 42a81b2

Please sign in to comment.