JS 8

  • Geolocation API

HTML Geolocation API allows the browser to make an API call on the user position:

//we can deconstruct the position argument to get the COORDinateS

navigator.geolocation.getCurrentPosition( async ( {coords: {latitude, longitude}}) =>{

    console.log("We are at lat " + latitude + " and lon " + longitude) 

  }, showError
);

/we can add a callback function for all the possible errors

function showError(error) {

    if(error){
      switch(error.code) {
        case error.PERMISSION_DENIED:
          user.innerHTML = "User denied the request for Geolocation."
          break;
        case error.POSITION_UNAVAILABLE:
          user.innerHTML = "Location information is unavailable."
          break;
        case error.TIMEOUT:
          user.innerHTML = "The request to get user location timed out."
          break;
        case error.UNKNOWN_ERROR:
          user.innerHTML = "An unknown error occurred."
          break;
      }    
    }
}

We can use Weatherbit APIarrow-up-right to fetch weather information:

We can also modify the fetch URL based on the data we have available:

We can also use openweathermap arrow-up-rightAPI to fetch cities with similar names:

Similar cities come with their weather and coordinates

We can use the Foursquare arrow-up-rightAPI to fetch cities locations:

Designing maps with MapBox

MapBox is a javascript library to build interactive maps using OpenGL to render in 2d or 3d.

First, we import MapBox.glarrow-up-right .js and .css:

We need an HTML id for the map:

For the javascript we need:

The mapbox Styles API includes map styles for the maps:

Markers are a method in mapboxgl.js, they rappresent a set of coordinates, with icon being the visual part:

To add custom markers we can add them as HTML tags on the map, or add them on the style:

We need a GEOjson with the coordinates features:

The Marker method can be used to add DIV tags and CSS class to the Map:

We just add a background-image as the icon

The Marker is added Inside the mapbox map

included with its own CSS class

Last updated