IONIC4获取基于IP地址的天气API

时间:2019-03-30 00:09:41

标签: typescript api ip ionic4 openweathermap

我正在做一个 IONIC4 项目,该项目需要根据用户 IP地址获取天气API 信息,因此移动用户可以无需激活 GPS 即可获取当前位置,这一切都是关于从互联网接收到的ip地址


import { HttpClient } from '@angular/common/http';
import { from } from 'rxjs';
import { ajax } from 'rxjs/ajax';
import { map, catchError } from 'rxjs/operators';
@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html', 
  styleUrls: ['home.page.scss'],
})
export class HomePage {
    weatherList;
    forcast;
    private baseUrl = 'https://api.openweathermap.org/data/2.5/';
    private countryIp = 'http://ip-api.com/json/?fields=country,city,zip,lat,lon,timezone,query';
    city;
    country;
    ip;
    constructor(
        public http: HttpClient) {
    }

    getip() {
        let url = this.countryIp; 
        return this.http.get(url, {});
    }

    getWeather() {
        this.city().subscribe(res =>{
            this.weatherList = res;
        }); 
    }

    viweForcast() {
        this.getforcast().subscribe((res : any[]) =>{
            this.forcast = res;
        });
        console.log(this.forcast);
     }

0 个答案:

没有答案