- Describe geocoding
- Use Mapbox geocoder to forward and reverse geocode
What is geocoding? - The process of converting a description of a place to geographic coordinates. The relationship between the description and the place is a mapping (no pun intended).
"Seattle, WA" -> geocode -> {lat: 47.6062095, lng: -122.3320708}
We'll use the Mapbox Geocoding API. Mapbox is a system of geolocation-related APIs, similar to the Google Maps API. Lyft, the Weather Channel, and many other large companies use Mapbox to incorporate maps into their apps.
- Head to the mapbox sign up page and make an account.
- Locate your Access Token - this will be the same for all Mapbox APIs
- Create a new node project called
geocode-example
. - Install the @mapbox/mapbox-sdk node module via
npm
. - In
mapTest.js
file, import the geocoder from the mapbox module and set up a geocoding client using your access token docs.
const mbxGeocoding = require('@mapbox/mapbox-sdk/services/geocoding');
const geocodingClient = mbxGeocoding({ accessToken: 'your-access-token' });
Search for 'Seattle, WA'
and check out the GeoJSON feature collection that is returned. docs
mapTest.js
geocodingClient
.forwardGeocode({
query: 'Seattle, WA'
})
.send()
.then(response => {
const match = response.body;
console.log(match);
});
The coordinates are listed under the center
field of each object.
geocodingClient
.forwardGeocode({
query: 'Seattle, WA'
})
.send()
.then(response => {
const match = response.body;
console.log(match.features[0].center);
});
Now copy the coordinates that you just found and reverse geocode them! docs
geocodingClient
.reverseGeocode({
query: [ -122.3301, 47.6038 ]
})
.send()
.then(response => {
const match = response.body;
console.log(match);
});
You're going to set up an app that allows users to search for a city and add their favorite cities to a database.
- Install
express
,ejs
,express-ejs-layouts
, andmethod-override
. - Set up an
express
app that listens to port8000
. - Add
ejs
,express-ejs-layouts
,body-parser
, andmethod-override
middleware. - Create your
views
folder that has yourlayout.ejs
.
- install
pg
andsequelize
via npm - initialize
sequelize
- configure
config.json
- create a
place
model withcity
,state
,lat
, andlong
fields - run migration
- form with two text inputs, one for city and one for state
- form should submit a GET request to
/search
- "View My Favorites" button links to
/favorites
view
- header that says "search results for "
- list search results (include the name and coordinates of each result, along with an "add to favorites" button)
- each list item should include a form with four hidden fields (city, state, lat, long) so the favorites button actually submits a
POST
request to/add
- include a "back to search" button that links back to the search page
- lists all saved favorite cities
- each list item should have a "remove from favorites" button that deletes that city from the
places
table and redirects back to the favorites page - "remove from favorites" button will need to be a
POST
form with a submit button that utilizesmethod-override
- render
city-search
view
- use forward geocoding to search for cities in the US (hint: use the
type
andcountries
fields in addition toquery
) - render
search-results
page, passing through the searched data as well as the results
- use
findOrCreate
to post to the database of favorites
- pull all favorited cities from the database and pass them into the view
- deletes city from
place
table and redirects to favorites view