Giter Site home page Giter Site logo

petefitton / mapbox-travelers Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wdi-sea/mapbox-travelers

0.0 1.0 0.0 29 KB

Use Mapbox to show travelers and their favorite cities using a many to many relationship with sequelize

JavaScript 38.97% HTML 61.03%

mapbox-travelers's Introduction

Geocoding with Mapbox

Objectives

  • Describe geocoding
  • Use Mapbox geocoder to forward and reverse geocode

Geocoding

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).

Example:

"Seattle, WA" -> geocode -> {lat: 47.6062095, lng: -122.3320708}

Mapbox

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.

Get Geocoding


Using Mapbox

  • Head to the mapbox sign up page and make an account.
  • Locate your Access Token - this will be the same for all Mapbox APIs

We'll be using @mapbox/mapbox-sdk node module via npm.

Set up

  • Run npm install.
  • 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' });

Forward Geocode

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);
  });

Reverse Geocode

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);
  });

More Resources


Exercise/Lab: City Search

You're going to set up an app that allows users to search for a city and add their favorite cities to a database.

Set up the front end.

  • Install express, ejs, express-ejs-layouts, and method-override.
  • Set up an express app that listens to port 8000.
  • Add ejs, express-ejs-layouts, and method-override middleware.
  • Create your views folder that has your layout.ejs.

Set up the database.

  • install pg, pg-hstore, and sequelize via npm
  • initialize sequelize
  • configure config.json
  • create a place model with city, state, lat, and long fields
  • run migration

Views

city-search

  • 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

search-results

  • 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

favorites

  • 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 utilizes method-override

Routes

GET '/'

  • render city-search view

GET '/search'

  • use forward geocoding to search for cities in the US (hint: use the type and countries fields in addition to query)
  • render search-results page, passing through the searched data as well as the results

POST '/add'

  • use findOrCreate to post to the database of favorites

GET '/favorites'

  • pull all favorited cities from the database and pass them into the view

DELETE '/remove'

  • deletes city from place table and redirects to favorites view

mapbox-travelers's People

Contributors

009kings avatar brandiw avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.