Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mauricio/Metro-LA/.jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
20 changes: 20 additions & 0 deletions mauricio/Metro-LA/css/style.css
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%;
}
Binary file added mauricio/Metro-LA/img/spinner.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions mauricio/Metro-LA/index.html
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>
95 changes: 95 additions & 0 deletions mauricio/Metro-LA/js/main.js
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>';
Copy link
Copy Markdown
Contributor

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

}
} catch (error) {
console.log(error);
}
}

function getIds(resRoutes) {
const array = resRoutes.items;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puedes hacer el return directamente!

return array.map(item => item.id);
}

function searchAndReplaceNumberVehicle(arrayIds, URL_ROUTE) {
let replaced = [];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto necesita un map y no un forEach... y lo sabes!

y-lo-sabes

arrayIds.forEach(item => {
replaced.push(URL_ROUTE.replace(/number/, item));
});
return replaced;
}

async function getAllVehicles(arrayUrlsParsed) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dale una vuelta a esta función... por un lado mezclas mucho async/await para luego devolver un promise.All que podrías haber gestionado muy distinto. Pero muy muy buen enfoque.

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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podrias dejarlo en una linea! y además tirar de array.from()


const coordinates = vehiclesConcat.map(item => {
return {
lat: item.latitude,
lng: item.longitude
};
});

initMap(coordinates);
}

function initMap(coordinates) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No es un mal patrón definir una inicialización como init 👍

})();
Binary file modified miguelGarcia/.DS_Store
Binary file not shown.