-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2314 from NCEAS/feature-1796-places-autocomplete
Feature 1796 places autocomplete
- Loading branch information
Showing
43 changed files
with
2,333 additions
and
495 deletions.
There are no files selected for viewing
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict'; | ||
|
||
define( | ||
['backbone', 'models/maps/GeoBoundingBox'], | ||
(Backbone, GeoBoundingBox) => { | ||
/** | ||
* @class GeocodedLocation | ||
* @classdes GeocodedLocation is the representation of a place that has been | ||
* geocoded to provide latitude and longitude bounding coordinates for | ||
* navigating to on a map. | ||
* @classcategory Models/Geocoder | ||
* @since x.x.x | ||
*/ | ||
const GeocodedLocation = Backbone.Model.extend({ | ||
/** | ||
* Overrides the default Backbone.Model.defaults() function to specify | ||
* default attributes. | ||
* @name GeocodedLocation#defaults | ||
* @type {Object} | ||
* @property {GeoBoundingBox} box Bounding box representing this location | ||
* on a map. | ||
* @property {string} displayName A name that can be displayed to the user | ||
* representing this location. | ||
*/ | ||
defaults() { | ||
return { | ||
box: new GeoBoundingBox, | ||
displayName: '', | ||
}; | ||
}, | ||
|
||
/** | ||
* @typedef {Object} GeocodedLocationOptions | ||
* @property {Object} box An object representing a boundary around a | ||
* location on a map. | ||
* @property {string} displayName A display name for the location. | ||
*/ | ||
initialize({ box: { north, south, east, west } = {}, displayName = '' } = {}) { | ||
this.set('box', new GeoBoundingBox({ north, south, east, west })); | ||
this.set('displayName', displayName); | ||
}, | ||
}); | ||
|
||
return GeocodedLocation; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use strict'; | ||
|
||
define( | ||
[ | ||
'models/geocoder/GoogleMapsGeocoder', | ||
'models/geocoder/GoogleMapsAutocompleter', | ||
], | ||
(GoogleMapsGeocoder, GoogleMapsAutocompleter) => { | ||
/** | ||
* GeocoderSearch interfaces with various geocoding and location | ||
* searching services. | ||
* @classcategory Models/Geocoder | ||
* @since x.x.x | ||
*/ | ||
class GeocoderSearch { | ||
/** | ||
* GoogleMapsAutocompleter model for interacting with Google Maps Places | ||
* Autocomplete APIs. | ||
*/ | ||
googleMapsAutocompleter = new GoogleMapsAutocompleter(); | ||
|
||
/** | ||
* GoogleMapsGeocoder for interacting with Google Maps Geocoder APIs. | ||
*/ | ||
googleMapsGeocoder = new GoogleMapsGeocoder(); | ||
|
||
/** | ||
* Convert a Google Maps Place ID into a list geocoded objects that can be | ||
* displayed in the map widget. | ||
* @param {string} newQuery - The user's input search query. | ||
* @returns {Prediction[]} An array of places that could be the result the | ||
* user is looking for. Most often this comes in five or less results. | ||
*/ | ||
async autocomplete(newQuery) { | ||
return this.googleMapsAutocompleter.autocomplete(newQuery); | ||
} | ||
|
||
/** | ||
* Convert a Google Maps Place ID into a list geocoded objects that can be | ||
* displayed in the map widget. | ||
* @param {Prediction} prediction An autocomplete prediction that includes | ||
* a unique identifier for geocoding. | ||
* @returns {GeocodedLocation[]} An array of locations with an associated | ||
* bounding box. According to Google Maps API this should most often be a | ||
* single value, but could potentially be many. | ||
*/ | ||
async geocode(prediction) { | ||
return this.googleMapsGeocoder.geocode(prediction); | ||
} | ||
} | ||
|
||
return GeocoderSearch; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
|
||
define( | ||
['backbone', 'gmaps', 'models/geocoder/Prediction'], | ||
(Backbone, gmaps, Prediction) => { | ||
/** | ||
* Integrate with the Google Maps Places Autocomplete API using the | ||
* Google Maps AutocompleteService JS library. | ||
* @classcategory Models/Geocoder | ||
* @since x.x.x | ||
*/ | ||
class GoogleMapsAutocompleter { | ||
/** | ||
* Google Maps service for interacting with the Places Autocomplete API. | ||
*/ | ||
autocompleter = new gmaps.places.AutocompleteService(); | ||
|
||
/** | ||
* Use the Google Maps Places API to get place predictions based off of a | ||
* user input string as the user types. | ||
* @param {string} input - User input to search for Google Maps places. | ||
* @returns {Prediction[]} An array of places that could be the result the | ||
* user is looking for. Most often this comes in five or less results. | ||
*/ | ||
async autocomplete(input) { | ||
if (!input) return []; | ||
const response = await this.autocompleter.getPlacePredictions({ | ||
input, | ||
}); | ||
return this.getPredictionsFromResults(response.predictions); | ||
} | ||
|
||
/** | ||
* Helper function that converts a Google Maps Autocomplete API result | ||
* into a useable Prediction model. | ||
* @param {Object[]} List of Google Maps Autocomplete API results. | ||
* @returns {Prediction[]} List of corresponding predictions. | ||
*/ | ||
getPredictionsFromResults(results) { | ||
return results.map(result => new Prediction({ | ||
description: result.description, | ||
googleMapsPlaceId: result.place_id, | ||
})); | ||
} | ||
} | ||
|
||
return GoogleMapsAutocompleter; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use strict'; | ||
|
||
define( | ||
['backbone', 'gmaps', 'models/geocoder/GeocodedLocation'], | ||
(Backbone, gmaps, GeocodedLocation) => { | ||
/** | ||
* Integrate with the Google Maps Geocoder API using the Google | ||
* Maps Geocoder JS library. | ||
* @classcategory Models/Geocoder | ||
* @since x.x.x | ||
*/ | ||
class GoogleMapsGeocoder { | ||
/** Google Maps service for interacting with the Geocoder API. */ | ||
geocoder = new gmaps.Geocoder(); | ||
|
||
/** | ||
* Use the Google Maps Geocoder API to convert a Google Maps Place ID into | ||
* a geocoded object that includes latitude and longitude information | ||
* along with a bound box for viewing the location. | ||
* @param {Prediction} prediction An autocomplete prediction that includes | ||
* a unique identifier for geocoding. | ||
* @returns {GeocodedLocation[]} An array of locations with an associated | ||
* bounding box. According to Google Maps API this should most often be a | ||
* single value, but could potentially be many. | ||
*/ | ||
async geocode(prediction) { | ||
const response = await this.geocoder.geocode({ | ||
placeId: prediction.get('googleMapsPlaceId') | ||
}); | ||
return this.getGeocodedLocationsFromResults(response.results); | ||
} | ||
|
||
/** | ||
* Helper function that converts a Google Maps Places API result into a | ||
* useable GeocodedLocation model. | ||
* @param {Object[]} List of Google Maps Places API results. | ||
* @returns {GeocodedLocation[]} List of corresponding geocoded locations. | ||
*/ | ||
getGeocodedLocationsFromResults(results) { | ||
return results.map(result => { | ||
return new GeocodedLocation({ | ||
box: result.geometry.viewport.toJSON(), | ||
displayName: result.address_components[0].long_name, | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
return GoogleMapsGeocoder; | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict'; | ||
|
||
define(['backbone'], (Backbone) => { | ||
/** | ||
* @class Prediction | ||
* @classdes Prediction represents a value returned from a location | ||
* autocompletion search. | ||
* @classcategory Models/Geocoder | ||
* @since x.x.x | ||
*/ | ||
const Prediction = Backbone.Model.extend({ | ||
/** | ||
* Overrides the default Backbone.Model.defaults() function to specify | ||
* default attributes for the Map | ||
* @name Prediction#defaults | ||
* @type {Object} | ||
* @property {string} description A user-friendly description of a Google | ||
* Maps Place. | ||
* @property {string} googleMapsPlaceId Unique identifier that can be | ||
* geocoded by the Google Maps Geocoder API. | ||
*/ | ||
defaults() { | ||
return { description: '', googleMapsPlaceId: '' }; | ||
}, | ||
|
||
/** | ||
* @typedef {Object} PredictionOptions | ||
* @property {string} description A string describing the location | ||
* represented by the Prediction. | ||
* @property {string} googleMapsPlaceId The place ID that is used to | ||
* uniquely identify a place in Google Maps API. | ||
*/ | ||
initialize({ description, googleMapsPlaceId, } = {}) { | ||
this.set('description', description); | ||
this.set('googleMapsPlaceId', googleMapsPlaceId); | ||
}, | ||
}); | ||
|
||
return Prediction; | ||
}); |
Oops, something went wrong.