-
Notifications
You must be signed in to change notification settings - Fork 11
Ejercicio metro L.A #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "esversion": 6 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| html, body { | ||
| height: 100%; | ||
| margin: 0; | ||
| padding: 0; | ||
| } | ||
|
|
||
| #map { | ||
| height: 100%; | ||
| } | ||
|
|
||
| #spinner{ | ||
| display: block; | ||
| background: url(../img/spinner.gif); | ||
| background-position: center center; | ||
| background-repeat: no-repeat; | ||
| background-origin: inherit; | ||
| position: absolute; | ||
| width: 100%; | ||
| height: 100%; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
|
|
||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
| <title>Metro LA - USA</title> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| <link rel="stylesheet" href="css/style.css"> | ||
| </head> | ||
|
|
||
| <body> | ||
|
|
||
| <div id="spinner"></div> | ||
| <div id="map"></div> | ||
|
|
||
| <script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script> | ||
| <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA8CXiCeg6Dvg0d9zDFUAFSPE0WsMsyQPc" type="text/javascript"></script> | ||
| <script src="./js/main.js"></script> | ||
| </body> | ||
|
|
||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| (function () { | ||
|
|
||
| const URL =`http://api.metro.net/agencies/lametro/routes/`; | ||
| const URL_ROUTE = `http://api.metro.net/agencies/lametro/routes/number/vehicles/`; | ||
|
|
||
| async function init(url) { | ||
| try { | ||
| const json = await fetch(url); | ||
| if (json.status !== 200) { | ||
| throw new Error('Error'); | ||
| } | ||
| const resRoutes = await json.json(); | ||
| if (resRoutes && resRoutes.items.length) { | ||
| const arrayIds = getIds(resRoutes); | ||
| const arrayUrls = searchAndReplaceNumberVehicle(arrayIds, URL_ROUTE); | ||
| const vehicles = await getAllVehicles(arrayUrls); | ||
| getAllRoutes(vehicles); | ||
| document.querySelector('#spinner').style.display = 'none'; | ||
| } else { | ||
| document.querySelector('#spinner').style.display = 'none'; | ||
| document.querySelector('#map').innerHTML = '<p style="position: absolute; transform: translate(-50%, -50%); top: 50%; left: 50%;">Algo ha ido mal, vuelve a intentarlo mas tarde</p>'; | ||
| } | ||
| } catch (error) { | ||
| console.log(error); | ||
| } | ||
| } | ||
|
|
||
| function getIds(resRoutes) { | ||
| const array = resRoutes.items; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Puedes hacer el |
||
| return array.map(item => item.id); | ||
| } | ||
|
|
||
| function searchAndReplaceNumberVehicle(arrayIds, URL_ROUTE) { | ||
| let replaced = []; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| arrayIds.forEach(item => { | ||
| replaced.push(URL_ROUTE.replace(/number/, item)); | ||
| }); | ||
| return replaced; | ||
| } | ||
|
|
||
| async function getAllVehicles(arrayUrlsParsed) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dale una vuelta a esta función... por un lado mezclas mucho |
||
| let promises = arrayUrlsParsed.map(async item => { | ||
| try{ | ||
| const json = await fetch(item); | ||
| if (json.status !== 200) { | ||
| throw new Error("Error"); | ||
| } | ||
| const data = await json.json(); | ||
| return data.items | ||
| } catch(error) { | ||
| console.log(error); | ||
| } | ||
| }); | ||
| return await Promise.all(promises); | ||
| }; | ||
|
|
||
| function getAllRoutes(vehicles) { | ||
| let vehiclesConcat = []; | ||
| vehiclesConcat = Array.prototype.concat.apply([], vehicles); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. podrias dejarlo en una linea! y además tirar de |
||
|
|
||
| const coordinates = vehiclesConcat.map(item => { | ||
| return { | ||
| lat: item.latitude, | ||
| lng: item.longitude | ||
| }; | ||
| }); | ||
|
|
||
| initMap(coordinates); | ||
| } | ||
|
|
||
| function initMap(coordinates) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bien enfocado! Me gusta que juntes las funcionalidades bajo nombres tan claros! 👍 |
||
| const map = new google.maps.Map(document.getElementById("map"), { | ||
| zoom: 11, | ||
| center: { | ||
| lat: 34.052235, | ||
| lng: -118.243683 | ||
| } | ||
| }); | ||
|
|
||
| const labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | ||
|
|
||
| const markers = coordinates.map(function (location, i) { | ||
| return new google.maps.Marker({ | ||
| position: location, | ||
| label: labels[i % labels.length] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sería más interesante tirar de información del propio medio de transporte... lat, lon, etc... |
||
| }); | ||
| }); | ||
|
|
||
| const markerCluster = new MarkerClusterer(map, markers, { | ||
| imagePath: "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m" | ||
| }); | ||
| } | ||
|
|
||
| init(URL); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No es un mal patrón definir una inicialización como |
||
| })(); | ||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casi mejor tener un div oculto por defecto con este mensaje y mostrarlo cuando el map no funcione