Coder Social home page Coder Social logo

geocoder's Introduction

Background & Objectives

In this exercise, we'll practise our AJAX skills. Let's start simple with a GET request. Here we'll use the MapBox Geocoding API. We want to build a tool where we can input an address, hit a button, and get the GPS Coordinates back! For the cherry on top, we'll display the map as well.

MapBox Geocoding demo

Specs

You can launch your local server with:

rake webpack

Geocoding

First, you will need to create an account with MapBox and get and API key (it's free to sign up!) Then, read the MapBox Geocoding API documentation. It boils down to doing an HTTP GET request with an address as a query string parameter.

'https://api.mapbox.com/geocoding/v5/mapbox.places/Los%20Angeles.json?access_token=YOUR-API-KEY'

NOTE: The request to the MapBox API will require your API key as one of the parameters in your request. You can find your key on your account page once you have created an account and signed in.

Go ahead and add a form to your HTML page. It should contain an input of type "text" where a user can type an address in, and an input of type "submit" to display a button.

Once that's done, use the submit event to catch the moment the form is posted by the user. That's when you'll want to trigger the AJAX query to the MapBox Geocoding service using fetch (go back to yesterday's lecture slides).

As always when you fetch data from an API, start by console.log()ing what you get back from MapBox. It's a massive JSON! Now you've got that, figure out where the GPS coordinates are buried and display them on screen.

HINT: Mapbox returns coordinates with longitude first, and latitude second!

[OPTIONAL] Displaying a map

To display a MapBox Map with a marker at the specified address, we'll use a second API, the MapBox JavaScript API.

To use it, add this line in the head of your HTML file, so you can use MapBox's stylesheet for your map:

<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.2.1/mapbox-gl.css' rel='stylesheet' />

To add a map, you'll need an empty supporting HTML element. For instance:

<div id="map" style="height: 300px; width: 600px"></div>

To easily build the map and add a marker to it, we'll use npm's mapbox-gl package.

You already have a package.json so you just need to yarn add mapbox-gl to download it locally in 02-Geocoder/node_modules.

To display a map in your #map with the mapbox-gl package you can use these lines:

import mapboxgl from 'mapbox-gl';

mapboxgl.accessToken = 'yourApiKey';
const map = new mapboxgl.Map({
  container: 'map',
  style: 'mapbox://styles/mapbox/streets-v9',
  center: [ -0.077, 51.533 ],
  zoom: 12
});

To add a marker to the map, if the variable map holds the mapboxgl object, you can run:

new mapboxgl.Marker()
  .setLngLat([ -0.077, 51.533 ])
  .addTo(map);

Happy geocoding! ๐ŸŒŽ ๐ŸŒ ๐ŸŒ

geocoder's People

Contributors

gabrielparaizo avatar

Stargazers

 avatar

Watchers

 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.