Esta es mi solución al reto.
sportspage.ts
@Component({
selector: 'app-sports',
templateUrl: './sports.page.html',
styleUrls: ['./sports.page.scss'],
})
export class SportsPage {
currentCenter: Coordinate;
coordinates: Coordinate[] = [];
defaultZoom = 14;
searching = false;
text = 'Enter keywords to perform a search.';
songs: any[];
song: any;
currentSong: HTMLAudioElement;
constructor(
private songService: SongService
) { }
ionViewDidEnter() {
this.getCurrentPosition();
this.watchPosition();
}
async getCurrentPosition() {
const coordinates = await Geolocation.getCurrentPosition();
this.currentCenter = {
latitude: coordinates.coords.latitude,
longitude: coordinates.coords.longitude
};
}
async watchPosition() {
Geolocation.watchPosition({}, pos => {
this.currentCenter = {
latitude: pos.coords.latitude,
longitude: pos.coords.longitude
};
this.coordinates.push(this.currentCenter);
});
}
async getTracks(keywords: string) {
this.searching = true;
if (keywords.length > 0) {
this.songService.searchTracks(keywords).subscribe(async resp => {
this.songs = await resp.tracks.items.filter((item: any) => item.preview_url);
if (this.songs.length === 0) {
this.text = 'There are no results for these keywords.';
}
this.searching = false;
});
} else {
this.text = 'Enter keywords to perform a search.';
this.songs = [];
}
}
play(song: any) {
if (this.currentSong) {
this.pause();
}
this.song = song;
this.currentSong = new Audio(this.song.preview_url);
this.currentSong.play();
this.currentSong.addEventListener('ended', () => this.song.playing = false);
this.song.playing = true;
}
pause() {
this.currentSong.pause();
this.song.playing = false;
}
}
sportpage.html
<ion-header>
<ion-toolbar>
<ion-title>Sports</ion-title>
<ion-buttons slot="start">
<ion-menu-button></ion-menu-button>
</ion-buttons>
</ion-toolbar>
</ion-header>
<ion-content>
<agm-map
*ngIf="currentCenter"
[zoom]="defaultZoom"
[latitude]="currentCenter.latitude"
[longitude]="currentCenter.longitude">
<agm-marker
[latitude]="currentCenter.latitude"
[longitude]="currentCenter.longitude"
iconUrl="assets/img/bicycle.png">
</agm-marker>
<agm-polyline [strokeColor]="'red'" *ngIf="coordinates">
<agm-polyline-point
*ngFor="let coordinate of coordinates"
[latitude]="coordinate.latitude"
[longitude]="coordinate.longitude"></agm-polyline-point>
</agm-polyline>
</agm-map>
<div class="ion-padding">
<ion-searchbar (keyup)="getTracks($event.target.value)"></ion-searchbar>
<h2>Tracks</h2>
<div class="ion-text-center" *ngIf="searching">
<ion-spinner name="crescent"></ion-spinner>
</div>
<div *ngIf="!songs || songs.length === 0">
<h3>Opps! There are no songs.</h3>
<ion-text>
{{ text }}
</ion-text>
</div>
<ion-virtual-scroll
[items]="songs"
*ngIf="songs">
<ion-item *virtualItem="let song">
{{ song.name }}
<ion-buttons slot="end">
<ion-button (click)="pause()" *ngIf="song.playing">
<ion-icon name="pause"></ion-icon>
</ion-button>
<ion-button *ngIf="song.playing">
<ion-spinner name="dots"></ion-spinner>
</ion-button>
<ion-button (click)="play(song)" *ngIf="!song.playing">
<ion-icon name="play"></ion-icon>
</ion-button>
</ion-buttons>
</ion-item>
</ion-virtual-scroll>
</div>
</ion-content>
¿Quieres ver más aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesión.