GithubHelp home page GithubHelp logo

muki0082 / ol-api-key-tut Goto Github PK

View Code? Open in Web Editor NEW
2.0 0.0 0.0 15 KB

Angular 4 application / tutorial - use data from an API with openlayers4, adding an API key

TypeScript 75.82% JavaScript 14.92% HTML 7.52% CSS 1.73%
javascript angular4 openlayers api api-key webcams-travel-api

ol-api-key-tut's Introduction

Openlayers and API data tutorial

A really really short tutorial, explaining how to use API data from an API that uses a key, with Openlayers

In this example I created an Angular 4 application, with Openlayers4. For our example API I used Webcams.travel API. The specification and different API links and usage can be found here: https://www.webcams.travel/

The API is really well constructed and works fast, if anyone wants to explore it further and use it in more complex app - I know I am ;)

The complete code is available in this repository as an working app. I will mention here only parts that are relevant for the purpose of this short tutorial.

  1. In this case, I was using a hosted library for Openlayers4 and also for jQuery. You will see below why we needed both of them.

So we have links in our index.html file like this:

    <link rel="stylesheet" href="https://openlayers.org/en/v4.5.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
        <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <!--  -->
    <script src="https://openlayers.org/en/v4.5.0/build/ol.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Because OpenLayers are not installed for this application, we need to declare the "ol" varaiable in our app.component.ts file:

    declare var ol: any;
  1. The OpenLayers elements for this example include a map, vector layer and a source for that layer:
    let vectorSource =  new ol.source.Vector({});

    let vectorLayer = new ol.layer.Vector({
        source: vectorSource,
        zIndex: 10
    });

    let center = ol.proj.transform([0, 0], 'EPSG:4326', 'EPSG:3857');

    let map = new ol.Map({
        view: new ol.View({
          center: center,
    			zoom: 3
        }),
        layers: [
    			new ol.layer.Tile({
            source: new ol.source.OSM(),
            zIndex: 1
          }),
          vectorLayer
        ],
        target: 'map'
    });
  1. At the end, we create a function, that will make AJAX request towards our API, and set a request header, sending the header name and the value. In this case, we are sending a X-Mashape-Key header, with our API key as a value.

To use AJAX in Angular, define jQuery in our app.component.ts file:

    declare var jquery: any;
    declare var $: any;

So, our success function would loop through every object gotten from the API, create a Point and then a Feature using the Point. After that, every Feature is added to our vector source.

for(let webcam of data.result.webcams) {
    let coordinates = new ol.geom.Point(ol.proj.transform([webcam.location.longitude, webcam.location.latitude], 'EPSG:4326', 'EPSG:3857'));

    let feature = new ol.Feature({
        name: 'Coordinates',
        geometry: coordinates
    });

    vectorSource.addFeature(feature);

The header data "injection" can be done in the beforeSend:

    beforeSend: function(xhr) {
        xhr.setRequestHeader("X-Mashape-Key", "YOUR-API-KEY");
    }

Which would cause sending header name and value to the API when requesting the data.

That's it, you now have some fresh API data in your vector source, and they are added to the OSM map with OpenLayers default styling ;)

TO-DO

  1. Use npm install openlayers and import ol modules, instead of using hosted libraries.

ol-api-key-tut's People

Contributors

angular-cli avatar muki0082 avatar

Stargazers

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